Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Nested classes support

Closes #89
Squash merge branch 'master' of https://github.com/wkalinin/swig into wkalinin-nested

By Vladimir Kalinin
* 'master' of https://github.com/wkalinin/swig:
  CPlusPlusOut mode for Octave
  nested class illustration
  fixed "Abstract" flag for nested classes added an example enabled anonymous nested structs runtime test
  porting
  warnings disabled
  porting fixes
  java runtime tests ported
  nested class closing bracket offset fixed
  removed double nested template (not supported by %template parsing)
  template_nested test extended
  parent field made public
  property access fixed
  replaced tabs with spaces
  warning W-reorder
  deprecated warnings removed, derived_nested runtime test added
  optimized string indenting
  Nested classes indenting
  nested classes docs
  fixed the order in which flattened inner classes are added after the outer
  Private nested classes were getting into the type table.
  Java getProxyName() fix for nested classes fixes the case when nested classes is forward declared
  Fix for a case when a nested class inherits from the same base as the outer. (Base class constructor declaration is found first in this case)
  merge fix
  nested C struct first immediate declaration incorrectly renamed sample fixed
  tests updated to reflect nested classes support
  Java nested classes support (1)
  flattening should remove the link to the outer class
  access mode correctly set/restored for nested classes
  nested templates should be skipped while flattening (template nodes themselves, not expanded versions) also non-public nested classes should be ignored
  If nested classes are not supported, default behaviour is flattening, not ignoring flag "nested" is preserved, so, the nested classes can be ignored by user
  nested workaround test updated
  template instantiated within a class is marked as nested for ignoring purposes
  %ignore not applied to the nested classed, because "nested" flag is set too late
  typedef name takes precedence over the real name (reason?)
  unnamed structs should be processed for all the languages
  nested C struct instances are wrapped as "immutable"
  tree building
  typedef declaration for unnamed C structures fixed
  nested classes "flattening"
  fixed %ignoring nested classes
  renamed "nested" attribute to "nested:outer" added "nested" flag, to be used with $ignore (it is not removed while flattening) added nestedClassesSupported() function to the Language interface
  renamed "nested" attribute to "nested:outer" added "nested" flag, to be used with $ignore (it is not removed while flattening) added nestedClassesSupported() function to the Language interface
  tree iteration fix
  dirclassname variable names unified memory issue fixed
  merge error
  ignore unnamed structs for C++
  unnamed nested C structs naming & unnesting
  class added to classes hash under typedef name
  private nested classes skipped
  test updated due to nested templates support
  anonymous structs with inheritance fixed nested_class test to allow anonymous structs w/o declarator
  tests updated: nested workaround removed from namespace_class.i propagated nested template declaration to the C++ file
  injected members scope
  nested tempplates fixes, nested structures in "C" mode parsing added utility function "appendSibling" (like "appendChild")
  nested unnamed structures parsing fixes, access mode restored on nested class end, tdname is properly patched with outer class name prefix
  memory management fixes
  nested templates (1)
  Nested unnamed structs
  Nested class support (1)
  Nested class support (1)
  • Loading branch information...
commit b63c4839fe60b8192809ef94d078ef07305144c5 1 parent fcd0480
@wkalinin wkalinin authored wsfulton committed
Showing with 1,445 additions and 1,107 deletions.
  1. +20 −124 Doc/Manual/SWIGPlus.html
  2. +1 −1  Examples/csharp/class/example.cxx
  3. +5 −3 Examples/csharp/class/example.h
  4. +3 −3 Examples/csharp/class/runme.cs
  5. +1 −1  Examples/java/class/example.cxx
  6. +5 −3 Examples/java/class/example.h
  7. +3 −3 Examples/java/class/runme.java
  8. +4 −6 Examples/test-suite/derived_nested.i
  9. +31 −31 Examples/test-suite/java/nested_class_runme.java
  10. +7 −7 Examples/test-suite/java/nested_structs_runme.java
  11. +4 −0 Examples/test-suite/java/template_nested_runme.java
  12. +0 −10 Examples/test-suite/namespace_class.i
  13. +0 −2  Examples/test-suite/namespace_union.i
  14. +2 −22 Examples/test-suite/nested_class.i
  15. +0 −2  Examples/test-suite/nested_comment.i
  16. +2 −18 Examples/test-suite/nested_workaround.i
  17. +10 −9 Examples/test-suite/template_nested.i
  18. +5 −1 Examples/test-suite/template_nested_typemaps.i
  19. +0 −1  Examples/test-suite/union_scope.i
  20. +1 −1  Lib/swig.swg
  21. +1 −0  Source/CParse/cparse.h
  22. +4 −0 Source/CParse/cscanner.c
  23. +243 −711 Source/CParse/parser.y
  24. +0 −2  Source/Include/swigwarn.h
  25. +4 −2 Source/Modules/allocate.cxx
  26. +4 −2 Source/Modules/contract.cxx
  27. +167 −64 Source/Modules/csharp.cxx
  28. +136 −58 Source/Modules/java.cxx
  29. +78 −17 Source/Modules/lang.cxx
  30. +43 −2 Source/Modules/main.cxx
  31. +1 −0  Source/Modules/octave.cxx
  32. +13 −0 Source/Modules/swigmod.h
  33. +456 −1 Source/Modules/typepass.cxx
  34. +65 −0 Source/Swig/naming.c
  35. +103 −0 Source/Swig/scanner.c
  36. +2 −0  Source/Swig/swig.h
  37. +1 −0  Source/Swig/swigscan.h
  38. +1 −0  Source/Swig/swigtree.h
  39. +19 −0 Source/Swig/tree.c
View
144 Doc/Manual/SWIGPlus.html
@@ -185,7 +185,6 @@
<ul>
<li>Overloaded versions of certain operators (new, delete, etc.)
-<li>Nested classes, see <a href="#SWIGPlus_nested_classes">Nested classes</a> for workarounds.
</ul>
<p>
@@ -4965,143 +4964,40 @@
<H2><a name="SWIGPlus_nested_classes"></a>6.27 Nested classes</H2>
-
-<p>
-There is some support for nested structs and unions when wrapping C code,
-see <a href="SWIG.html#SWIG_nested_structs">Nested structures</a> for further details.
-The added complexity of C++ compared to C means this approach does not work well for
-C++ code (when using the -c++ command line option).
-For C++, a nested class is treated much like an opaque pointer, so anything useful within the nested class, such as its
-methods and variables, are not accessible from the target language.
-True nested class support may be added to SWIG in the future, however,
-until then some of the following workarounds can be applied to improve the situation.
-</p>
-
<p>
-It might be possible to use partial class information as often you can accept that the nested class is not needed,
-especially if it is not actually used in any methods you need from the target language.
-Imagine you are wrapping the following <tt>Outer</tt> class which contains a nested class <tt>Inner</tt>.
-The easiest thing to do is turn a blind eye to the warning that SWIG generates, or simply suppress it:
+If the target language supports the nested classes concept (like Java), the nested C++ classes
+are wrapped as nested target language proxy classes. (In case of Java - "static" nested classes.)
+Only public nested classes are wrapped. Otherwise there is little difference between nested and
+normal classes.
</p>
-
-<div class="code">
-<pre>
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::Inner;
-
-class Outer {
-public:
- class Inner {
- public:
- ...
- };
- Inner getInner();
- void useInner(const Inner&amp; inner);
- ...
-};
-</pre>
-</div>
-
-<p>
-Note that if <tt>Inner</tt> can be used as an opaque type, the default wrapping approach suffices.
-For example, if the nested class does not need to be created from the target language, but can be obtained via a method
-call, such as the <tt>getInner()</tt> method above, the returned value can then be passed around, such as passed into the
-<tt>useInner()</tt> method.
-</p>
-
-<p>
-With some more effort the above situation can be improved somewhat and a nested class can be constructed and used
-from the target language much like any other non-nested class. Assuming we have the <tt>Outer</tt> class in a header file:
-</p>
-
-<div class="code">
-<pre>
-// File outer.h
-class Outer {
-public:
- class Inner {
- public:
- int var;
- Inner(int v = 0) : var(v) {}
- };
- Inner getInner();
- void useInner(const Inner&amp; inner);
-};
-</pre>
-</div>
-
<p>
-The following interface file works around the nested class limitations by redefining the nested class as a global class.
-A typedef for the compiler and the <tt>nestedworkaround</tt>
-<a href="Customization.html#Customization_feature_flags">feature flag</a> is also required in
-order for the generated wrappers to compile. This flag simply removes all the type information from SWIG, so SWIG treats
-the nested class as if it had not been parsed at all.
+If the target language doesn't support nested classes directly, or the support is not implemented in the
+language module (like for python currently), then the visible nested classes are moved to the same name
+space as the containing class (nesting hierarchy is "flattened"). The same behaviour may be turned on for
+C# and Java by the %feature ("flatnested"); If there is a class with the same name in the outer namespace
+the inner class (or the global one) may be renamed or ignored:
</p>
<div class="code">
<pre>
-// File : example.i
-%module example
-
-// Redefine nested class in global scope in order for SWIG to generate
-// a proxy class. Only SWIG parses this definition.
-class Inner {
+%rename (Bar_Foo) Bar::Foo;
+class Foo {};
+class Bar {
public:
- int var;
- Inner(int v = 0) : var(v) {}
-};
-
-%nestedworkaround Outer::Inner;
-
-%{
-#include "outer.h"
-%}
-%include "outer.h"
-
-// We've fooled SWIG into thinking that Inner is a global class, so now we need
-// to trick the C++ compiler into understanding this apparent global type.
-%{
-typedef Outer::Inner Inner;
-%}
-</pre>
-</div>
-
-<p>
-The downside to this approach is a more complex interface file and having to maintain two definitions of <tt>Inner</tt>,
-the real one and the one in the interface file that SWIG parses.
-However, the upside is that all the methods/variables in the nested class are available from the target language
-as a proxy class is generated instead of treating the nested class as an opaque type.
-The proxy class can be constructed from the target language and passed into any methods accepting the nested class.
-Also note that the original header file is parsed unmodified.
-</p>
-
-<p>
-Finally, conditional compilation can be used as a workaround to comment out nested class definitions in the actual headers,
-assuming you are able to modify them.
-</p>
-
-<div class="code">
-<pre>
-// File outer.h
-class Outer {
-public:
-#ifndef SWIG
- class Inner {
- public:
- ...
- };
-#endif
- ...
+ class Foo {};
};
</pre>
</div>
-<p>
-This workaround used to be common when SWIG could not deal with nested classes particulary well.
-This should just be a last resort for unusual corner cases now as SWIG can parse nested classes and even handle nested template classes fairly well.
-</p>
<p>
-<b>Compatibility Note:</b> SWIG-1.3.40 and earlier versions did not have the <tt>nestedworkaround</tt> feature
+<b>Compatibility Note:</b>
+In SWIG 2.0 and earlier, nested classes were treated as opaque pointers.
+Also there was a workaround, implementing approximately the same behaviour as the
+%feature ("flatnested") with an additional help from the user:
+nested class had to be manually redeclared in the global scope, typedef name and %feature nestedworkaround
+added for the inner class.
+SWIG-1.3.40 and earlier versions did not have the <tt>nestedworkaround</tt> feature
and the generated code resulting from parsing nested classes did not always compile.
Nested class warnings could also not be suppressed using %warnfilter.
</p>
View
2  Examples/csharp/class/example.cxx
@@ -9,7 +9,7 @@ void Shape::move(double dx, double dy) {
y += dy;
}
-int Shape::nshapes = 0;
+int Shape::Counter::nshapes = 0;
double Circle::area(void) {
return M_PI*radius*radius;
View
8 Examples/csharp/class/example.h
@@ -2,17 +2,19 @@
class Shape {
public:
+ struct Counter{
+ static int nshapes;
+ };
Shape() {
- nshapes++;
+ Counter::nshapes++;
}
virtual ~Shape() {
- nshapes--;
+ Counter::nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
- static int nshapes;
};
class Circle : public Shape {
View
6 Examples/csharp/class/runme.cs
@@ -17,9 +17,9 @@ static void Main()
Console.WriteLine( " Created circle " + c );
Console.WriteLine( " Created square " + s );
- // ----- Access a static member -----
+ // ----- Access a static member of a nested class -----
- Console.WriteLine( "\nA total of " + Shape.nshapes + " shapes were created" );
+ Console.WriteLine( "\nA total of " + Shape.Counter.nshapes + " shapes were created" );
// ----- Member data access -----
@@ -60,7 +60,7 @@ static void Main()
// Note: when this using scope is exited the C# Dispose() methods
// are called which in turn call the C++ destructors
- Console.WriteLine( Shape.nshapes + " shapes remain" );
+ Console.WriteLine( Shape.Counter.nshapes + " shapes remain" );
Console.WriteLine( "Goodbye" );
}
}
View
2  Examples/java/class/example.cxx
@@ -9,7 +9,7 @@ void Shape::move(double dx, double dy) {
y += dy;
}
-int Shape::nshapes = 0;
+int Shape::Counter::nshapes = 0;
double Circle::area(void) {
return M_PI*radius*radius;
View
8 Examples/java/class/example.h
@@ -2,17 +2,19 @@
class Shape {
public:
+ struct Counter{
+ static int nshapes;
+ };
Shape() {
- nshapes++;
+ Counter::nshapes++;
}
virtual ~Shape() {
- nshapes--;
+ Counter::nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
- static int nshapes;
};
class Circle : public Shape {
View
6 Examples/java/class/runme.java
@@ -21,9 +21,9 @@ public static void main(String argv[])
Square s = new Square(10);
System.out.println( " Created square " + s );
- // ----- Access a static member -----
+ // ----- Access a static member of a nested class -----
- System.out.println( "\nA total of " + Shape.getNshapes() + " shapes were created" );
+ System.out.println( "\nA total of " + Shape.Counter.getNshapes() + " shapes were created" );
// ----- Member data access -----
@@ -64,7 +64,7 @@ public static void main(String argv[])
c.delete();
s.delete();
- System.out.println( Shape.getNshapes() + " shapes remain" );
+ System.out.println( Shape.Counter.getNshapes() + " shapes remain" );
System.out.println( "Goodbye" );
}
}
View
10 Examples/test-suite/derived_nested.i
@@ -3,14 +3,12 @@ This was reported in bug #909389 */
%module derived_nested
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) BB::CC;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) BB::DD;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) BB::EE;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) BB::FF;
-
%inline %{
-class A { int x; };
+class A {
+public:
+ int x;
+};
class B {
class C { int y; }; //generates a warning
class D : public A { int z; }; //ok
View
62 Examples/test-suite/java/nested_class_runme.java
@@ -14,59 +14,59 @@
public static void main(String argv[]) {
Outer outer = new Outer();
- SWIGTYPE_p_Outer__InnerStruct1 is1 = outer.makeInnerStruct1();
- SWIGTYPE_p_Outer__InnerClass1 ic1 = outer.makeInnerClass1();
- SWIGTYPE_p_Outer__InnerUnion1 iu1 = outer.makeInnerUnion1();
+ Outer.InnerStruct1 is1 = outer.makeInnerStruct1();
+ Outer.InnerClass1 ic1 = outer.makeInnerClass1();
+ Outer.InnerUnion1 iu1 = outer.makeInnerUnion1();
- SWIGTYPE_p_Outer__InnerStruct2 is2 = outer.makeInnerStruct2();
- SWIGTYPE_p_Outer__InnerClass2 ic2 = outer.makeInnerClass2();
- SWIGTYPE_p_Outer__InnerUnion2 iu2 = outer.makeInnerUnion2();
+ Outer.InnerStruct2 is2 = outer.makeInnerStruct2();
+ Outer.InnerClass2 ic2 = outer.makeInnerClass2();
+ Outer.InnerUnion2 iu2 = outer.makeInnerUnion2();
- SWIGTYPE_p_Outer__InnerClass4Typedef ic4 = outer.makeInnerClass4Typedef();
- SWIGTYPE_p_Outer__InnerStruct4Typedef is4 = outer.makeInnerStruct4Typedef();
- SWIGTYPE_p_Outer__InnerUnion4Typedef iu4 = outer.makeInnerUnion4Typedef();
+ Outer.InnerClass4Typedef ic4 = outer.makeInnerClass4Typedef();
+ Outer.InnerStruct4Typedef is4 = outer.makeInnerStruct4Typedef();
+ Outer.InnerUnion4Typedef iu4 = outer.makeInnerUnion4Typedef();
- SWIGTYPE_p_Outer__InnerClass5 ic5 = outer.makeInnerClass5();
- SWIGTYPE_p_Outer__InnerStruct5 is5 = outer.makeInnerStruct5();
- SWIGTYPE_p_Outer__InnerUnion5 iu5 = outer.makeInnerUnion5();
+ Outer.InnerClass5Typedef ic5 = outer.makeInnerClass5();
+ Outer.InnerStruct5Typedef is5 = outer.makeInnerStruct5();
+ Outer.InnerUnion5Typedef iu5 = outer.makeInnerUnion5();
ic5 = outer.makeInnerClass5Typedef();
is5 = outer.makeInnerStruct5Typedef();
iu5 = outer.makeInnerUnion5Typedef();
{
- SWIGTYPE_p_Outer__InnerMultiple im1 = outer.getMultipleInstance1();
- SWIGTYPE_p_Outer__InnerMultiple im2 = outer.getMultipleInstance2();
- SWIGTYPE_p_Outer__InnerMultiple im3 = outer.getMultipleInstance3();
- SWIGTYPE_p_Outer__InnerMultiple im4 = outer.getMultipleInstance4();
+ Outer.InnerMultiple im1 = outer.getMultipleInstance1();
+ Outer.InnerMultiple im2 = outer.getMultipleInstance2();
+ Outer.InnerMultiple im3 = outer.getMultipleInstance3();
+ Outer.InnerMultiple im4 = outer.getMultipleInstance4();
}
{
- SWIGTYPE_p_Outer__InnerMultipleDerived im1 = outer.getMultipleDerivedInstance1();
- SWIGTYPE_p_Outer__InnerMultipleDerived im2 = outer.getMultipleDerivedInstance2();
- SWIGTYPE_p_Outer__InnerMultipleDerived im3 = outer.getMultipleDerivedInstance3();
- SWIGTYPE_p_Outer__InnerMultipleDerived im4 = outer.getMultipleDerivedInstance4();
+ Outer.InnerMultipleDerived im1 = outer.getMultipleDerivedInstance1();
+ Outer.InnerMultipleDerived im2 = outer.getMultipleDerivedInstance2();
+ Outer.InnerMultipleDerived im3 = outer.getMultipleDerivedInstance3();
+ Outer.InnerMultipleDerived im4 = outer.getMultipleDerivedInstance4();
}
{
- SWIGTYPE_p_Outer__InnerMultipleDerived im1 = outer.getMultipleDerivedInstance1();
- SWIGTYPE_p_Outer__InnerMultipleDerived im2 = outer.getMultipleDerivedInstance2();
- SWIGTYPE_p_Outer__InnerMultipleDerived im3 = outer.getMultipleDerivedInstance3();
- SWIGTYPE_p_Outer__InnerMultipleDerived im4 = outer.getMultipleDerivedInstance4();
+ Outer.InnerMultipleDerived im1 = outer.getMultipleDerivedInstance1();
+ Outer.InnerMultipleDerived im2 = outer.getMultipleDerivedInstance2();
+ Outer.InnerMultipleDerived im3 = outer.getMultipleDerivedInstance3();
+ Outer.InnerMultipleDerived im4 = outer.getMultipleDerivedInstance4();
}
{
- SWIGTYPE_p_Outer__InnerMultipleAnonTypedef1 mat1 = outer.makeInnerMultipleAnonTypedef1();
- SWIGTYPE_p_Outer__InnerMultipleAnonTypedef2 mat2 = outer.makeInnerMultipleAnonTypedef2();
- SWIGTYPE_p_Outer__InnerMultipleAnonTypedef3 mat3 = outer.makeInnerMultipleAnonTypedef3();
+ Outer.InnerMultipleAnonTypedef1 mat1 = outer.makeInnerMultipleAnonTypedef1();
+ Outer.InnerMultipleAnonTypedef1 mat2 = outer.makeInnerMultipleAnonTypedef2();
+ SWIGTYPE_p_p_Outer__InnerMultipleAnonTypedef1 mat3 = outer.makeInnerMultipleAnonTypedef3();
- SWIGTYPE_p_Outer__InnerMultipleNamedTypedef mnt = outer.makeInnerMultipleNamedTypedef();
- SWIGTYPE_p_Outer__InnerMultipleNamedTypedef mnt1 = outer.makeInnerMultipleNamedTypedef1();
- SWIGTYPE_p_Outer__InnerMultipleNamedTypedef mnt2 = outer.makeInnerMultipleNamedTypedef2();
+ Outer.InnerMultipleNamedTypedef1 mnt = outer.makeInnerMultipleNamedTypedef();
+ Outer.InnerMultipleNamedTypedef1 mnt1 = outer.makeInnerMultipleNamedTypedef1();
+ Outer.InnerMultipleNamedTypedef1 mnt2 = outer.makeInnerMultipleNamedTypedef2();
SWIGTYPE_p_p_Outer__InnerMultipleNamedTypedef mnt3 = outer.makeInnerMultipleNamedTypedef3();
}
{
- SWIGTYPE_p_Outer__InnerSameName isn = outer.makeInnerSameName();
+ Outer.InnerSameName isn = outer.makeInnerSameName();
}
}
}
View
14 Examples/test-suite/java/nested_structs_runme.java
@@ -17,18 +17,18 @@ public static void main(String argv[]) {
nested_structs.setValues(outer, 10);
Outer_inner1 inner1 = outer.getInner1();
- Outer_inner2 inner2 = outer.getInner2();
- Outer_inner3 inner3 = outer.getInner3();
- Outer_inner4 inner4 = outer.getInner4();
+ Outer_inner1 inner2 = outer.getInner2();
+ Outer_inner1 inner3 = outer.getInner3();
+ Outer_inner1 inner4 = outer.getInner4();
if (inner1.getVal() != 10) throw new RuntimeException("failed inner1");
if (inner2.getVal() != 20) throw new RuntimeException("failed inner2");
if (inner3.getVal() != 20) throw new RuntimeException("failed inner3");
if (inner4.getVal() != 40) throw new RuntimeException("failed inner4");
- Outer_inside1 inside1 = outer.getInside1();
- Outer_inside2 inside2 = outer.getInside2();
- Outer_inside3 inside3 = outer.getInside3();
- Outer_inside4 inside4 = outer.getInside4();
+ Named inside1 = outer.getInside1();
+ Named inside2 = outer.getInside2();
+ Named inside3 = outer.getInside3();
+ Named inside4 = outer.getInside4();
if (inside1.getVal() != 100) throw new RuntimeException("failed inside1");
if (inside2.getVal() != 200) throw new RuntimeException("failed inside2");
if (inside3.getVal() != 200) throw new RuntimeException("failed inside3");
View
4 Examples/test-suite/java/template_nested_runme.java
@@ -25,6 +25,10 @@ public static void main(String argv[]) {
T_NestedOuterTemplateDouble tn = new T_NestedOuterTemplateDouble();
if (tn.hohum(-12.3) != -12.3)
throw new RuntimeException("it failed");
+ OuterClass.T_OuterClassInner1Int inner1 = new OuterClass().useInner1(new OuterClass.T_OuterClassInner1Int());
+ OuterClass.T_OuterClassInner2NormalClass inner2 = new OuterClass.T_OuterClassInner2NormalClass();
+ inner2.setEmbeddedVar(2);
+ OuterClass.T_OuterClassInner2NormalClass inner22 = new OuterClass().useInner2Again(inner2);
}
}
View
10 Examples/test-suite/namespace_class.i
@@ -1,6 +1,5 @@
%module namespace_class
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Ala::Ola;
#ifdef SWIGD
%warnfilter(SWIGWARN_IGNORE_OPERATOR_LT);
@@ -216,9 +215,6 @@ namespace a
%}
-// %copyctor doesn't work with nested class workaround
-%nocopyctor;
-
%inline %{
class Ala {
public :
@@ -236,12 +232,6 @@ namespace a
};
%}
-%rename(Ala__Ola) Ala::Ola;
-class Ala::Ola {
-public:
- Ola() {}
- void eek() {}
-};
%template(hi) Ala::hi<int>;
View
2  Examples/test-suite/namespace_union.i
@@ -1,7 +1,5 @@
%module namespace_union
-#pragma SWIG nowarn=SWIGWARN_PARSE_UNNAMED_NESTED_CLASS
-
%inline %{
namespace SpatialIndex
{
View
24 Examples/test-suite/nested_class.i
@@ -1,25 +1,5 @@
%module nested_class
-#pragma SWIG nowarn=SWIGWARN_PARSE_UNNAMED_NESTED_CLASS
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerStruct1;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerClass1;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerUnion1;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerClass2;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerStruct2;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerUnion2;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerClass4Typedef;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerStruct4Typedef;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerUnion4Typedef;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerClass5;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerStruct5;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerUnion5;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerMultiple;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerMultipleDerived;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerMultipleAnonTypedef1;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerMultipleNamedTypedef;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer::InnerSameName;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) Outer2::IgnoreMe;
-
%inline %{
struct Outer {
typedef int Integer;
@@ -39,7 +19,7 @@ struct Outer {
};
///////////////////////////////////////////
-#ifdef SWIG
+#if defined(__GNUC__) || defined(_MSC_VER) || defined(SWIG)
/* some compilers do not accept these */
class {
public:
@@ -154,7 +134,7 @@ struct Outer {
Integer xx;
} MultipleInstanceAnonDerived1, MultipleInstanceAnonDerived2, *MultipleInstanceAnonDerived3, MultipleInstanceAnonDerived4[2];
-#ifdef SWIG
+#if defined(__GNUC__) || defined(_MSC_VER) || defined(SWIG)
/* some compilers do not accept these */
struct : public InnerMultiple {
Integer xx;
View
2  Examples/test-suite/nested_comment.i
@@ -1,7 +1,5 @@
%module nested_comment
-#pragma SWIG nowarn=SWIGWARN_PARSE_UNNAMED_NESTED_CLASS
-
// this example shows a problem with 'dump_nested' (parser.y).
// bug #949654
View
20 Examples/test-suite/nested_workaround.i
@@ -1,14 +1,6 @@
%module nested_workaround
-// Similar to "Nested classes" documentation example.
-
-class Inner {
- int val;
- public:
- Inner(int v = 0) : val(v) {}
- void setValue(int v) { val = v; }
- int getValue() const { return val; }
-};
-%nestedworkaround Outer::Inner;
+// "flatnested" emulates deprecated feature "nested_workaround" for the languages not supporting nested classes
+%feature ("flatnested");
%inline %{
class Outer {
@@ -28,11 +20,3 @@ public:
}
};
%}
-
-// We've fooled SWIG into thinking that Inner is a global class, so now we need
-// to trick the C++ compiler into understanding this apparent global type.
-%{
-typedef Outer::Inner Inner;
-%}
-
-
View
19 Examples/test-suite/template_nested.i
@@ -2,13 +2,6 @@
// Test nested templates - that is template classes and template methods within a class.
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) ns::OuterClass::Inner1;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) ns::OuterClass::Inner2;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) ns::OuterTemplate::NestedInnerTemplate1;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) ns::OuterTemplate::NestedInnerTemplate2;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) ns::OuterTemplate::NestedInnerTemplate3;
-%warnfilter(SWIGWARN_PARSE_NAMED_NESTED_CLASS) ns::OuterTemplate::NestedStruct;
-
namespace ns {
template <class T> struct ForwardTemplate;
}
@@ -33,7 +26,13 @@ namespace ns {
template <class T> struct NormalTemplate {
void tmethod(T t) {}
};
+}
+%}
+%template(T_NormalTemplateNormalClass) ns::NormalTemplate<ns::NormalClass>;
+%template(T_NormalTemplateInt) ns::NormalTemplate<int>;
+%inline %{
+namespace ns {
class OuterClass {
public:
template <class T> struct Inner1 {
@@ -70,6 +69,7 @@ namespace ns {
};
};
Inner2<int> useInner2(const Inner2<int>& inner) { return inner; }
+ Inner2<NormalClass> useInner2Again(const Inner2<NormalClass>& inner) { return inner; }
int iii;
};
struct ABC {
@@ -108,9 +108,10 @@ namespace ns {
%}
-%template(T_NormalTemplateNormalClass) ns::NormalTemplate<ns::NormalClass>;
%template(T_OuterTMethodNormalClass) ns::OuterClass::InnerTMethod<ns::NormalClass>;
%template(T_TemplateFuncs1Int) ns::TemplateFuncs::templateMethod1<int>;
%template(T_TemplateFuncs2Double) ns::TemplateFuncs::templateMethod2<double>;
%template(T_NestedOuterTemplateDouble) ns::OuterTemplate<double>;
-
+%template(T_OuterClassInner1Int) ns::OuterClass::Inner1<int>;
+%template(T_OuterClassInner2NormalClass) ns::OuterClass::Inner2<ns::NormalClass>;
+%template(T_OuterClassInner2Int) ns::OuterClass::Inner2<int>;
View
6 Examples/test-suite/template_nested_typemaps.i
@@ -4,18 +4,22 @@
// Testing that the typemaps invoked within a class via %template are picked up by appropriate methods
+%inline %{
template <typename T> struct Typemap {
+#ifdef SWIG
%typemap(in) T {
$1 = -99;
}
+#endif
};
template <> struct Typemap<short> { // Note explicit specialization
+#ifdef SWIG
%typemap(in) short {
$1 = -77;
}
+#endif
};
-%inline %{
int globalInt1(int s) { return s; }
short globalShort1(short s) { return s; }
View
1  Examples/test-suite/union_scope.i
@@ -2,7 +2,6 @@
%warnfilter(SWIGWARN_RUBY_WRONG_NAME) nRState; // Ruby, wrong class name
%warnfilter(SWIGWARN_RUBY_WRONG_NAME) nRState_rstate; // Ruby, wrong class name
-#pragma SWIG nowarn=SWIGWARN_PARSE_UNNAMED_NESTED_CLASS
%inline %{
class nRState {
View
2  Lib/swig.swg
@@ -302,7 +302,7 @@ static int NAME(TYPE x) {
%define %$ismemberset "match$memberset"="1" %enddef
%define %$classname %$ismember,"match$parentNode$name" %enddef
-
+%define %$isnested "match$nested"="1" %enddef
/* -----------------------------------------------------------------------------
* Include all the warnings labels and macros
* ----------------------------------------------------------------------------- */
View
1  Source/CParse/cparse.h
@@ -31,6 +31,7 @@ extern "C" {
extern void scanner_file(File *);
extern void scanner_next_token(int);
extern void skip_balanced(int startchar, int endchar);
+ extern String *get_raw_text_balanced(int startchar, int endchar);
extern void skip_decl(void);
extern void scanner_check_typedef(void);
extern void scanner_ignore_typedef(void);
View
4 Source/CParse/cscanner.c
@@ -118,6 +118,10 @@ void skip_balanced(int startchar, int endchar) {
return;
}
+String* get_raw_text_balanced(int startchar, int endchar) {
+ return Scanner_get_raw_text_balanced(scan, startchar, endchar);
+}
+
/* ----------------------------------------------------------------------------
* void skip_decl(void)
*
View
954 Source/CParse/parser.y
@@ -51,7 +51,7 @@ static Node *module_node = 0;
static String *Classprefix = 0;
static String *Namespaceprefix = 0;
static int inclass = 0;
-static int nested_template = 0; /* template class/function definition within a class */
+static Node *currentOuterClass = 0; /*for nested classes*/
static char *last_cpptype = 0;
static int inherit_list = 0;
static Parm *template_parameters = 0;
@@ -59,10 +59,7 @@ static int extendmode = 0;
static int compact_default_args = 0;
static int template_reduce = 0;
static int cparse_externc = 0;
-
-static int max_class_levels = 0;
-static int class_level = 0;
-static Node **class_decl = NULL;
+extern int CPlusPlusOut;
/* -----------------------------------------------------------------------------
* Assist Functions
@@ -165,7 +162,6 @@ static Node *copy_node(Node *n) {
static char *typemap_lang = 0; /* Current language setting */
static int cplus_mode = 0;
-static String *class_rename = 0;
/* C++ modes */
@@ -237,6 +233,25 @@ static String *feature_identifier_fix(String *s) {
}
}
+static void set_access_mode(Node* n) {
+ if (cplus_mode == CPLUS_PUBLIC)
+ Setattr(n, "access", "public");
+ else if (cplus_mode == CPLUS_PROTECTED)
+ Setattr(n, "access", "protected");
+ else
+ Setattr(n, "access", "private");
+}
+
+static void restore_access_mode(Node* n) {
+ char* mode = Char(Getattr(n, "access"));
+ if (strcmp(mode, "private") == 0)
+ cplus_mode = CPLUS_PRIVATE;
+ else if (strcmp(mode, "protected") == 0)
+ cplus_mode = CPLUS_PROTECTED;
+ else
+ cplus_mode = CPLUS_PUBLIC;
+}
+
/* Generate the symbol table name for an object */
/* This is a bit of a mess. Need to clean up */
static String *add_oldname = 0;
@@ -283,13 +298,6 @@ static void add_symbols(Node *n) {
String *decl;
String *wrn = 0;
- if (nested_template) {
- if (!(n && Equal(nodeType(n), "template"))) {
- return;
- }
- /* continue if template function, but not template class, declared within a class */
- }
-
if (inclass && n) {
cparse_normalize_void(n);
}
@@ -349,9 +357,6 @@ static void add_symbols(Node *n) {
Delete(prefix);
}
- /*
- if (!Getattr(n,"parentNode") && class_level) set_parentNode(n,class_decl[class_level - 1]);
- */
Setattr(n,"ismember","1");
}
}
@@ -793,53 +798,31 @@ static String *make_class_name(String *name) {
return nname;
}
-static List *make_inherit_list(String *clsname, List *names) {
- int i, ilen;
- String *derived;
- List *bases = NewList();
-
- if (Namespaceprefix) derived = NewStringf("%s::%s", Namespaceprefix,clsname);
- else derived = NewString(clsname);
-
- ilen = Len(names);
- for (i = 0; i < ilen; i++) {
- Node *s;
- String *base;
- String *n = Getitem(names,i);
- /* Try to figure out where this symbol is */
- s = Swig_symbol_clookup(n,0);
- if (s) {
- while (s && (Strcmp(nodeType(s),"class") != 0)) {
- /* Not a class. Could be a typedef though. */
- String *storage = Getattr(s,"storage");
- if (storage && (Strcmp(storage,"typedef") == 0)) {
- String *nn = Getattr(s,"type");
- s = Swig_symbol_clookup(nn,Getattr(s,"sym:symtab"));
- } else {
- break;
- }
- }
- if (s && ((Strcmp(nodeType(s),"class") == 0) || (Strcmp(nodeType(s),"template") == 0))) {
- String *q = Swig_symbol_qualified(s);
- Append(bases,s);
- if (q) {
- base = NewStringf("%s::%s", q, Getattr(s,"name"));
- Delete(q);
- } else {
- base = NewString(Getattr(s,"name"));
- }
- } else {
- base = NewString(n);
- }
- } else {
- base = NewString(n);
- }
- if (base) {
- Swig_name_inherit(base,derived);
- Delete(base);
+/* Use typedef name as class name */
+void add_typedef_name(Node* n, Node* decl, String* oldName, Symtab *cscope, String* scpname)
+{
+ String* class_rename = 0;
+ SwigType *decltype = Getattr(decl,"decl");
+ if (!decltype || !Len(decltype)) {
+ String *cname;
+ String *tdscopename;
+ String *class_scope = Swig_symbol_qualifiedscopename(cscope);
+ String *name = Getattr(decl,"name");
+ cname = Copy(name);
+ Setattr(n,"tdname",cname);
+ tdscopename = class_scope ? NewStringf("%s::%s", class_scope, name) : Copy(name);
+ class_rename = Getattr(n, "class_rename");
+ if (class_rename && (Strcmp(class_rename,oldName) == 0))
+ Setattr(n, "class_rename", NewString(name));
+ if (!classes_typedefs) classes_typedefs = NewHash();
+ if (!Equal(scpname, tdscopename) && !Getattr(classes_typedefs, tdscopename)) {
+ Setattr(classes_typedefs, tdscopename, n);
}
+ Setattr(n,"decl",decltype);
+ Delete(class_scope);
+ Delete(cname);
+ Delete(tdscopename);
}
- return bases;
}
/* If the class name is qualified. We need to create or lookup namespace entries */
@@ -1059,350 +1042,31 @@ static String *resolve_create_node_scope(String *cname) {
return cname;
}
-
-
-/* Structures for handling code fragments built for nested classes */
-
-typedef struct Nested {
- String *code; /* Associated code fragment */
- int line; /* line number where it starts */
- const char *name; /* Name associated with this nested class */
- const char *kind; /* Kind of class */
- int unnamed; /* unnamed class */
- SwigType *type; /* Datatype associated with the name */
- struct Nested *next; /* Next code fragment in list */
-} Nested;
-
-/* Some internal variables for saving nested class information */
-
-static Nested *nested_list = 0;
-
-/* Add a function to the nested list */
-
-static void add_nested(Nested *n) {
- if (!nested_list) {
- nested_list = n;
- } else {
- Nested *n1 = nested_list;
- while (n1->next)
- n1 = n1->next;
- n1->next = n;
- }
-}
-
-/* -----------------------------------------------------------------------------
- * nested_new_struct()
- *
- * Nested struct handling for C code only creates a global struct from the nested struct.
- *
- * Nested structure. This is a sick "hack". If we encounter
- * a nested structure, we're going to grab the text of its definition and
- * feed it back into the scanner. In the meantime, we need to grab
- * variable declaration information and generate the associated wrapper
- * code later. Yikes!
- *
- * This really only works in a limited sense. Since we use the
- * code attached to the nested class to generate both C code
- * it can't have any SWIG directives in it. It also needs to be parsable
- * by SWIG or this whole thing is going to puke.
- * ----------------------------------------------------------------------------- */
-
-static void nested_new_struct(const char *kind, String *struct_code, Node *cpp_opt_declarators) {
- String *name;
- String *decl;
-
- /* Create a new global struct declaration which is just a copy of the nested struct */
- Nested *nested = (Nested *) malloc(sizeof(Nested));
- Nested *n = nested;
-
- name = Getattr(cpp_opt_declarators, "name");
- decl = Getattr(cpp_opt_declarators, "decl");
-
- n->code = NewStringEmpty();
- Printv(n->code, "typedef ", kind, " ", struct_code, " $classname_", name, ";\n", NIL);
- n->name = Swig_copy_string(Char(name));
- n->line = cparse_start_line;
- n->type = NewStringEmpty();
- n->kind = kind;
- n->unnamed = 0;
- SwigType_push(n->type, decl);
- n->next = 0;
-
- /* Repeat for any multiple instances of the nested struct */
- {
- Node *p = cpp_opt_declarators;
- p = nextSibling(p);
- while (p) {
- Nested *nn = (Nested *) malloc(sizeof(Nested));
-
- name = Getattr(p, "name");
- decl = Getattr(p, "decl");
-
- nn->code = NewStringEmpty();
- Printv(nn->code, "typedef ", kind, " ", struct_code, " $classname_", name, ";\n", NIL);
- nn->name = Swig_copy_string(Char(name));
- nn->line = cparse_start_line;
- nn->type = NewStringEmpty();
- nn->kind = kind;
- nn->unnamed = 0;
- SwigType_push(nn->type, decl);
- nn->next = 0;
- n->next = nn;
- n = nn;
- p = nextSibling(p);
- }
- }
-
- add_nested(nested);
-}
-
-/* -----------------------------------------------------------------------------
- * nested_forward_declaration()
- *
- * Nested struct handling for C++ code only.
- *
- * Treat the nested class/struct/union as a forward declaration until a proper
- * nested class solution is implemented.
- * ----------------------------------------------------------------------------- */
-
-static Node *nested_forward_declaration(const char *storage, const char *kind, String *sname, String *name, Node *cpp_opt_declarators) {
- Node *nn = 0;
- int warned = 0;
-
- if (sname) {
- /* Add forward declaration of the nested type */
- Node *n = new_node("classforward");
- Setattr(n, "kind", kind);
- Setattr(n, "name", sname);
- Setattr(n, "storage", storage);
- Setattr(n, "sym:weak", "1");
- add_symbols(n);
- nn = n;
- }
-
- /* Add any variable instances. Also add in any further typedefs of the nested type.
- Note that anonymous typedefs (eg typedef struct {...} a, b;) are treated as class forward declarations */
- if (cpp_opt_declarators) {
- int storage_typedef = (storage && (strcmp(storage, "typedef") == 0));
- int variable_of_anonymous_type = !sname && !storage_typedef;
- if (!variable_of_anonymous_type) {
- int anonymous_typedef = !sname && (storage && (strcmp(storage, "typedef") == 0));
- Node *n = cpp_opt_declarators;
- SwigType *type = name;
- while (n) {
- Setattr(n, "type", type);
- Setattr(n, "storage", storage);
- if (anonymous_typedef) {
- Setattr(n, "nodeType", "classforward");
- Setattr(n, "sym:weak", "1");
- }
- n = nextSibling(n);
- }
- add_symbols(cpp_opt_declarators);
-
- if (nn) {
- set_nextSibling(nn, cpp_opt_declarators);
- } else {
- nn = cpp_opt_declarators;
+/* look for simple typedef name in typedef list */
+String* try_to_find_a_name_for_unnamed_structure(char* storage, Node* decls) {
+ String* name = 0;
+ Node* n = decls;
+ if (storage && (strcmp(storage,"typedef") == 0)) {
+ for (; n; n = nextSibling(n)) {
+ if (!Len(Getattr(n, "decl"))) {
+ name = Copy(Getattr(n, "name"));
+ break;
}
}
}
-
- if (nn && Equal(nodeType(nn), "classforward")) {
- Node *n = nn;
- if (GetFlag(n, "feature:nestedworkaround")) {
- Swig_symbol_remove(n);
- nn = 0;
- warned = 1;
- } else {
- SWIG_WARN_NODE_BEGIN(n);
- Swig_warning(WARN_PARSE_NAMED_NESTED_CLASS, cparse_file, cparse_line,"Nested %s not currently supported (%s ignored)\n", kind, sname ? sname : name);
- SWIG_WARN_NODE_END(n);
- warned = 1;
- }
- }
-
- if (!warned)
- Swig_warning(WARN_PARSE_UNNAMED_NESTED_CLASS, cparse_file, cparse_line, "Nested %s not currently supported (ignored).\n", kind);
-
- return nn;
+ return name;
}
-/* Strips C-style and C++-style comments from string in-place. */
-static void strip_comments(char *string) {
- int state = 0; /*
- * 0 - not in comment
- * 1 - in c-style comment
- * 2 - in c++-style comment
- * 3 - in string
- * 4 - after reading / not in comments
- * 5 - after reading * in c-style comments
- * 6 - after reading \ in strings
- */
- char * c = string;
- while (*c) {
- switch (state) {
- case 0:
- if (*c == '\"')
- state = 3;
- else if (*c == '/')
- state = 4;
- break;
- case 1:
- if (*c == '*')
- state = 5;
- *c = ' ';
- break;
- case 2:
- if (*c == '\n')
- state = 0;
- else
- *c = ' ';
- break;
- case 3:
- if (*c == '\"')
- state = 0;
- else if (*c == '\\')
- state = 6;
- break;
- case 4:
- if (*c == '/') {
- *(c-1) = ' ';
- *c = ' ';
- state = 2;
- } else if (*c == '*') {
- *(c-1) = ' ';
- *c = ' ';
- state = 1;
- } else
- state = 0;
- break;
- case 5:
- if (*c == '/')
- state = 0;
- else
- state = 1;
- *c = ' ';
- break;
- case 6:
- state = 3;
- break;
- }
- ++c;
- }
-}
-
-/* Dump all of the nested class declarations to the inline processor
- * However. We need to do a few name replacements and other munging
- * first. This function must be called before closing a class! */
-
-static Node *dump_nested(const char *parent) {
- Nested *n,*n1;
- Node *ret = 0;
- Node *last = 0;
- n = nested_list;
- if (!parent) {
- nested_list = 0;
- return 0;
- }
- while (n) {
- Node *retx;
- SwigType *nt;
- /* Token replace the name of the parent class */
- Replace(n->code, "$classname", parent, DOH_REPLACE_ANY);
-
- /* Fix up the name of the datatype (for building typedefs and other stuff) */
- Append(n->type,parent);
- Append(n->type,"_");
- Append(n->type,n->name);
-
- /* Add the appropriate declaration to the C++ processor */
- retx = new_node("cdecl");
- Setattr(retx,"name",n->name);
- nt = Copy(n->type);
- Setattr(retx,"type",nt);
- Delete(nt);
- Setattr(retx,"nested",parent);
- if (n->unnamed) {
- Setattr(retx,"unnamed","1");
- }
-
- add_symbols(retx);
- if (ret) {
- set_nextSibling(last, retx);
- Delete(retx);
- } else {
- ret = retx;
- }
- last = retx;
-
- /* Strip comments - further code may break in presence of comments. */
- strip_comments(Char(n->code));
-
- /* Make all SWIG created typedef structs/unions/classes unnamed else
- redefinition errors occur - nasty hack alert.*/
-
- {
- const char* types_array[3] = {"struct", "union", "class"};
- int i;
- for (i=0; i<3; i++) {
- char* code_ptr = Char(n->code);
- while (code_ptr) {
- /* Replace struct name (as in 'struct name {...}' ) with whitespace
- name will be between struct and opening brace */
-
- code_ptr = strstr(code_ptr, types_array[i]);
- if (code_ptr) {
- char *open_bracket_pos;
- code_ptr += strlen(types_array[i]);
- open_bracket_pos = strchr(code_ptr, '{');
- if (open_bracket_pos) {
- /* Make sure we don't have something like struct A a; */
- char* semi_colon_pos = strchr(code_ptr, ';');
- if (!(semi_colon_pos && (semi_colon_pos < open_bracket_pos)))
- while (code_ptr < open_bracket_pos)
- *code_ptr++ = ' ';
- }
- }
- }
- }
- }
-
- {
- /* Remove SWIG directive %constant which may be left in the SWIG created typedefs */
- char* code_ptr = Char(n->code);
- while (code_ptr) {
- code_ptr = strstr(code_ptr, "%constant");
- if (code_ptr) {
- char* directive_end_pos = strchr(code_ptr, ';');
- if (directive_end_pos) {
- while (code_ptr <= directive_end_pos)
- *code_ptr++ = ' ';
- }
- }
- }
- }
- {
- Node *newnode = new_node("insert");
- String *code = NewStringEmpty();
- Wrapper_pretty_print(n->code, code);
- Setattr(newnode,"code", code);
- Delete(code);
- set_nextSibling(last, newnode);
- Delete(newnode);
- last = newnode;
- }
-
- /* Dump the code to the scanner */
- start_inline(Char(Getattr(last, "code")),n->line);
-
- n1 = n->next;
- Delete(n->code);
- free(n);
- n = n1;
+/* traverse copied tree segment, and update outer class links*/
+void update_nested_classes(Node* n)
+{
+ Node* c = firstChild(n);
+ while (c) {
+ if (Getattr(c, "nested:outer"))
+ Setattr(c, "nested:outer", n);
+ update_nested_classes(c);
+ c = nextSibling(c);
}
- nested_list = 0;
- return ret;
}
Node *Swig_cparse(File *f) {
@@ -1768,7 +1432,7 @@ static void tag_nodes(Node *n, const_String_or_char_ptr attrname, DOH *value) {
%type <node> cpp_declaration cpp_class_decl cpp_forward_class_decl cpp_template_decl cpp_alternate_rettype;
%type <node> cpp_members cpp_member;
%type <node> cpp_constructor_decl cpp_destructor_decl cpp_protection_decl cpp_conversion_operator cpp_static_assert;
-%type <node> cpp_swig_directive cpp_temp_possible cpp_nested cpp_opt_declarators ;
+%type <node> cpp_swig_directive cpp_temp_possible /*cpp_nested*/ cpp_opt_declarators ;
%type <node> cpp_using_decl cpp_namespace_decl cpp_catch_decl cpp_lambda_decl;
%type <node> kwargs options;
@@ -2999,6 +2663,7 @@ template_directive: SWIGTEMPLATE LPAREN idstringopt RPAREN idcolonnt LESSTHAN va
}
templnode = copy_node(nn);
+ update_nested_classes(templnode);
/* We need to set the node name based on name used to instantiate */
Setattr(templnode,"name",tname);
Delete(tname);
@@ -3007,7 +2672,7 @@ template_directive: SWIGTEMPLATE LPAREN idstringopt RPAREN idcolonnt LESSTHAN va
} else {
Setattr(templnode,"sym:typename","1");
}
- if ($3 && !inclass) {
+ if ($3) {
/*
Comment this out for 1.3.28. We need to
re-enable it later but first we need to
@@ -3026,16 +2691,15 @@ template_directive: SWIGTEMPLATE LPAREN idstringopt RPAREN idcolonnt LESSTHAN va
Setattr(templnode,"sym:name",nname);
Delete(nname);
Setattr(templnode,"feature:onlychildren", "typemap,typemapitem,typemapcopy,typedef,types,fragment");
-
- if ($3) {
- Swig_warning(WARN_PARSE_NESTED_TEMPLATE, cparse_file, cparse_line, "Named nested template instantiations not supported. Processing as if no name was given to %%template().\n");
- }
}
Delattr(templnode,"templatetype");
Setattr(templnode,"template",nn);
Setfile(templnode,cparse_file);
Setline(templnode,cparse_line);
Delete(temparms);
+ if (currentOuterClass) {
+ SetFlag(templnode, "nested");
+ }
add_symbols_copy(templnode);
@@ -3051,7 +2715,7 @@ template_directive: SWIGTEMPLATE LPAREN idstringopt RPAREN idcolonnt LESSTHAN va
csyms = Swig_symbol_current();
Swig_symbol_setscope(Getattr(templnode,"symtab"));
if (baselist) {
- List *bases = make_inherit_list(Getattr(templnode,"name"),baselist);
+ List *bases = Swig_make_inherit_list(Getattr(templnode,"name"),baselist, Namespaceprefix);
if (bases) {
Iterator s;
for (s = First(bases); s.item; s = Next(s)) {
@@ -3704,10 +3368,10 @@ cpp_declaration : cpp_class_decl { $$ = $1; }
/* A simple class/struct/union definition */
cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
- if (nested_template == 0) {
String *prefix;
List *bases = 0;
Node *scope = 0;
+ String *code;
$<node>$ = new_node("class");
Setline($<node>$,cparse_start_line);
Setattr($<node>$,"kind",$2);
@@ -3719,41 +3383,27 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
Setattr($<node>$,"allows_typedef","1");
/* preserve the current scope */
- prev_symtab = Swig_symbol_current();
+ Setattr($<node>$,"prev_symtab",Swig_symbol_current());
/* If the class name is qualified. We need to create or lookup namespace/scope entries */
scope = resolve_create_node_scope($3);
Setfile(scope,cparse_file);
Setline(scope,cparse_line);
$3 = scope;
-
- /* support for old nested classes "pseudo" support, such as:
-
- %rename(Ala__Ola) Ala::Ola;
- class Ala::Ola {
- public:
- Ola() {}
- };
-
- this should disappear when a proper implementation is added.
- */
- if (nscope_inner && Strcmp(nodeType(nscope_inner),"namespace") != 0) {
- if (Namespaceprefix) {
- String *name = NewStringf("%s::%s", Namespaceprefix, $3);
- $3 = name;
- Namespaceprefix = 0;
- nscope_inner = 0;
- }
- }
Setattr($<node>$,"name",$3);
- Delete(class_rename);
- class_rename = make_name($<node>$,$3,0);
+ if (currentOuterClass) {
+ SetFlag($<node>$, "nested");
+ Setattr($<node>$, "nested:outer", currentOuterClass);
+ set_access_mode($<node>$);
+ }
+ /* save yyrename to the class attribute, to be used later in add_symbols()*/
+ Setattr($<node>$, "class_rename", make_name($<node>$, $3, 0));
+ Setattr($<node>$, "Classprefix", $3);
Classprefix = NewString($3);
/* Deal with inheritance */
- if ($4) {
- bases = make_inherit_list($3,Getattr($4,"public"));
- }
+ if ($4)
+ bases = Swig_make_inherit_list($3,Getattr($4,"public"),Namespaceprefix);
prefix = SwigType_istemplate_templateprefix($3);
if (prefix) {
String *fbase, *tbase;
@@ -3775,18 +3425,7 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
}
Swig_symbol_newscope();
Swig_symbol_setscopename($3);
- if (bases) {
- Iterator s;
- for (s = First(bases); s.item; s = Next(s)) {
- Symtab *st = Getattr(s.item,"symtab");
- if (st) {
- Setfile(st,Getfile(s.item));
- Setline(st,Getline(s.item));
- Swig_symbol_inherit(st);
- }
- }
- Delete(bases);
- }
+ Swig_inherit_base_symbols(bases);
Delete(Namespaceprefix);
Namespaceprefix = Swig_symbol_qualifiedscopename(0);
cparse_start_line = cparse_line;
@@ -3805,31 +3444,27 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
Delete(tpname);
}
}
- if (class_level >= max_class_levels) {
- if (!max_class_levels) {
- max_class_levels = 16;
- } else {
- max_class_levels *= 2;
- }
- class_decl = (Node**) realloc(class_decl, sizeof(Node*) * max_class_levels);
- if (!class_decl) {
- Swig_error(cparse_file, cparse_line, "realloc() failed\n");
- }
- }
- class_decl[class_level++] = $<node>$;
Delete(prefix);
inclass = 1;
- }
+ currentOuterClass = $<node>$;
+ if (CPlusPlusOut) { /* save the structure declaration to declare it in global scope for C++ to see*/
+ code = get_raw_text_balanced('{', '}');
+ Setattr($<node>$, "code", code);
+ Delete(code);
+ }
} cpp_members RBRACE cpp_opt_declarators {
- (void) $<node>6;
- if (nested_template == 0) {
Node *p;
SwigType *ty;
- Symtab *cscope = prev_symtab;
+ Symtab *cscope;
Node *am = 0;
String *scpname = 0;
- $$ = class_decl[--class_level];
- inclass = 0;
+ (void) $<node>6;
+ $$ = currentOuterClass;
+ currentOuterClass = Getattr($$, "nested:outer");
+ if (!currentOuterClass)
+ inclass = 0;
+ cscope = Getattr($$, "prev_symtab");
+ Delattr($$, "prev_symtab");
/* Check for pure-abstract class */
Setattr($$,"abstracts", pure_abstracts($7));
@@ -3855,7 +3490,10 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
p = $9;
if (p) {
- set_nextSibling($$,p);
+ if (!cparse_cplusplus && currentOuterClass)
+ appendChild(currentOuterClass, p);
+ else
+ appendSibling($$, p);
}
if (cparse_cplusplus && !cparse_externc) {
@@ -3866,41 +3504,14 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
while (p) {
Setattr(p,"storage",$1);
Setattr(p,"type",ty);
- p = nextSibling(p);
- }
- /* Class typedefs */
- {
- String *name = $3;
- if ($9) {
- SwigType *decltype = Getattr($9,"decl");
- if (Cmp($1,"typedef") == 0) {
- if (!decltype || !Len(decltype)) {
- String *cname;
- String *tdscopename;
- String *class_scope = Swig_symbol_qualifiedscopename(cscope);
- name = Getattr($9,"name");
- cname = Copy(name);
- Setattr($$,"tdname",cname);
- tdscopename = class_scope ? NewStringf("%s::%s", class_scope, name) : Copy(name);
-
- /* Use typedef name as class name */
- if (class_rename && (Strcmp(class_rename,$3) == 0)) {
- Delete(class_rename);
- class_rename = NewString(name);
- }
- if (!classes_typedefs) classes_typedefs = NewHash();
- if (!Equal(scpname, tdscopename) && !Getattr(classes_typedefs, tdscopename)) {
- Setattr(classes_typedefs, tdscopename, $$);
- }
- Setattr($$,"decl",decltype);
- Delete(class_scope);
- Delete(cname);
- Delete(tdscopename);
- }
- }
+ if (!cparse_cplusplus) {
+ SetFlag(p,"hasconsttype");
+ SetFlag(p,"feature:immutable");
}
- appendChild($$,dump_nested(Char(name)));
+ p = nextSibling(p);
}
+ if ($9 && Cmp($1,"typedef") == 0)
+ add_typedef_name($$, $9, $3, cscope, scpname);
Delete(scpname);
if (cplus_mode != CPLUS_PUBLIC) {
@@ -3912,10 +3523,13 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
appendChild($$,pa);
Delete(pa);
}
+ if (currentOuterClass)
+ restore_access_mode($$);
Setattr($$,"symtab",Swig_symbol_popscope());
- Classprefix = 0;
+ Classprefix = Getattr($<node>$,"Classprefix");
+ Delattr($<node>$,"Classprefix");
if (nscope_inner) {
/* this is tricky */
/* we add the declaration in the original namespace */
@@ -3932,37 +3546,59 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
add_symbols($9);
} else {
Delete(yyrename);
- yyrename = Copy(class_rename);
+ yyrename = 0;
Delete(Namespaceprefix);
Namespaceprefix = Swig_symbol_qualifiedscopename(0);
-
- add_symbols($$);
- add_symbols($9);
+ if (!cparse_cplusplus && currentOuterClass) { /* nested C structs go into global scope*/
+ Node* outer = currentOuterClass;
+ while (Getattr(outer, "nested:outer"))
+ outer = Getattr(outer, "nested:outer");
+ appendSibling(outer, $$);
+ add_symbols($9);
+ set_scope_to_global();
+ Delete(Namespaceprefix);
+ Namespaceprefix = Swig_symbol_qualifiedscopename(0);
+ yyrename = Copy(Getattr($<node>$, "class_rename"));
+ add_symbols($$);
+ if (!CPlusPlusOut)
+ Delattr($$, "nested:outer");
+ Delattr($$, "class_rename");
+ $$ = 0;
+ } else {
+ yyrename = Copy(Getattr($<node>$, "class_rename"));
+ add_symbols($$);
+ add_symbols($9);
+ Delattr($$, "class_rename");
+ }
}
Swig_symbol_setscope(cscope);
Delete(Namespaceprefix);
Namespaceprefix = Swig_symbol_qualifiedscopename(0);
- } else {
- $$ = new_node("class");
- Setattr($$,"kind",$2);
- Setattr($$,"name",NewString($3));
- SetFlag($$,"nestedtemplateclass");
- }
}
/* An unnamed struct, possibly with a typedef */
- | storage_class cpptype LBRACE {
+ | storage_class cpptype inherit LBRACE {
String *unnamed;
+ String *code;
unnamed = make_unnamed();
$<node>$ = new_node("class");
Setline($<node>$,cparse_start_line);
Setattr($<node>$,"kind",$2);
+ if ($3) {
+ Setattr($<node>$,"baselist", Getattr($3,"public"));
+ Setattr($<node>$,"protectedbaselist", Getattr($3,"protected"));
+ Setattr($<node>$,"privatebaselist", Getattr($3,"private"));
+ }
Setattr($<node>$,"storage",$1);
Setattr($<node>$,"unnamed",unnamed);
Setattr($<node>$,"allows_typedef","1");
- Delete(class_rename);
- class_rename = make_name($<node>$,0,0);
+ if (currentOuterClass) {
+ SetFlag($<node>$, "nested");
+ Setattr($<node>$, "nested:outer", currentOuterClass);
+ set_access_mode($<node>$);
+ }
+ Setattr($<node>$, "class_rename", make_name($<node>$,0,0));
if (strcmp($2,"class") == 0) {
cplus_mode = CPLUS_PRIVATE;
} else {
@@ -3970,108 +3606,111 @@ cpp_class_decl : storage_class cpptype idcolon inherit LBRACE {
}
Swig_symbol_newscope();
cparse_start_line = cparse_line;
- if (class_level >= max_class_levels) {
- if (!max_class_levels) {
- max_class_levels = 16;
- } else {
- max_class_levels *= 2;
- }
- class_decl = (Node**) realloc(class_decl, sizeof(Node*) * max_class_levels);
- if (!class_decl) {
- Swig_error(cparse_file, cparse_line, "realloc() failed\n");
- }
- }
- class_decl[class_level++] = $<node>$;
+ currentOuterClass = $<node>$;
inclass = 1;
Classprefix = NewStringEmpty();
Delete(Namespaceprefix);
Namespaceprefix = Swig_symbol_qualifiedscopename(0);
- } cpp_members RBRACE declarator initializer c_decl_tail {
+ /* save the structure declaration to make a typedef for it later*/
+ code = get_raw_text_balanced('{', '}');
+ Setattr($<node>$, "code", code);
+ Delete(code);
+ } cpp_members RBRACE cpp_opt_declarators {
String *unnamed;
+ List *bases = 0;
+ String *name = 0;
Node *n;
- (void) $<node>4;
Classprefix = 0;
- $$ = class_decl[--class_level];
- inclass = 0;
+ $$ = currentOuterClass;
+ currentOuterClass = Getattr($$, "nested:outer");
+ if (!currentOuterClass)
+ inclass = 0;
+ else
+ restore_access_mode($$);
unnamed = Getattr($$,"unnamed");
-
- /* Check for pure-abstract class */
- Setattr($$,"abstracts", pure_abstracts($5));
-
- n = new_node("cdecl");
- Setattr(n,"name",$7.id);
- Setattr(n,"unnamed",unnamed);
- Setattr(n,"type",unnamed);
- Setattr(n,"decl",$7.type);
- Setattr(n,"parms",$7.parms);
- Setattr(n,"storage",$1);
- if ($9) {
- Node *p = $9;
- set_nextSibling(n,p);
- while (p) {
- String *type = Copy(unnamed);
- Setattr(p,"name",$7.id);
- Setattr(p,"unnamed",unnamed);
- Setattr(p,"type",type);
- Delete(type);
- Setattr(p,"storage",$1);
- p = nextSibling(p);
- }
- }
- set_nextSibling($$,n);
- Delete(n);
- {
+ /* Check for pure-abstract class */
+ Setattr($$,"abstracts", pure_abstracts($6));
+ n = $8;
+ if (n) {
+ appendSibling($$,n);
/* If a proper typedef name was given, we'll use it to set the scope name */
- String *name = 0;
- if ($1 && (strcmp($1,"typedef") == 0)) {
- if (!Len($7.type)) {
- String *scpname = 0;
- name = $7.id;
- Setattr($$,"tdname",name);
- Setattr($$,"name",name);
- Swig_symbol_setscopename(name);
+ name = try_to_find_a_name_for_unnamed_structure($1, n);
+ if (name) {
+ String *scpname = 0;
+ SwigType *ty;
+ Setattr($$,"tdname",name);
+ Setattr($$,"name",name);
+ Swig_symbol_setscopename(name);
+ if ($3)
+ bases = Swig_make_inherit_list(name,Getattr($3,"public"),Namespaceprefix);
+ Swig_inherit_base_symbols(bases);
/* If a proper name was given, we use that as the typedef, not unnamed */
- Clear(unnamed);
- Append(unnamed, name);
-
+ Clear(unnamed);
+ Append(unnamed, name);
+ if (cparse_cplusplus && !cparse_externc) {
+ ty = NewString(name);
+ } else {
+ ty = NewStringf("%s %s", $2,name);
+ }
+ while (n) {
+ Setattr(n,"storage",$1);
+ Setattr(n, "type", ty);
+ if (!cparse_cplusplus) {
+ SetFlag(n,"hasconsttype");
+ SetFlag(n,"feature:immutable");
+ }
n = nextSibling(n);
- set_nextSibling($$,n);
-
+ }
+ n = $8;
/* Check for previous extensions */
- if (extendhash) {
- String *clsname = Swig_symbol_qualifiedscopename(0);
- Node *am = Getattr(extendhash,clsname);
- if (am) {
+ if (extendhash) {
+ String *clsname = Swig_symbol_qualifiedscopename(0);
+ Node *am = Getattr(extendhash,clsname);
+ if (am) {
/* Merge the extension into the symbol table */
- merge_extensions($$,am);
- append_previous_extension($$,am);
- Delattr(extendhash,clsname);
- }
- Delete(clsname);
+ merge_extensions($$,am);
+ append_previous_extension($$,am);
+ Delattr(extendhash,clsname);
}
- if (!classes) classes = NewHash();
- scpname = Swig_symbol_qualifiedscopename(0);
- Setattr(classes,scpname,$$);
- Delete(scpname);
- } else {
- Swig_symbol_setscopename("<unnamed>");
+ Delete(clsname);
+ }
+ if (!classes) classes = NewHash();
+ scpname = Swig_symbol_qualifiedscopename(0);
+ Setattr(classes,scpname,$$);
+ Delete(scpname);
+ } else { /* no suitable name was found for a struct */
+ Setattr($$, "nested:unnamed", Getattr(n, "name")); /* save the name of the first declarator for later use in name generation*/
+ while (n) { /* attach unnamed struct to the declarators, so that they would receive proper type later*/
+ Setattr(n, "nested:unnamedtype", $$);
+ Setattr(n, "storage", $1);
+ n = nextSibling(n);
}
+ n = $8;
+ Swig_symbol_setscopename("<unnamed>");
}
- appendChild($$,$5);
- appendChild($$,dump_nested(Char(name)));
- }
- /* Pop the scope */
- Setattr($$,"symtab",Swig_symbol_popscope());
- if (class_rename) {
- Delete(yyrename);
- yyrename = NewString(class_rename);
+ appendChild($$,$6);
+ /* Pop the scope */
+ Setattr($$,"symtab",Swig_symbol_popscope());
+ if (name) {
+ Delete(yyrename);
+ yyrename = Copy(Getattr($<node>$, "class_rename"));
+ Delete(Namespaceprefix);
+ Namespaceprefix = Swig_symbol_qualifiedscopename(0);
+ add_symbols($$);
+ add_symbols(n);
+ Delattr($$, "class_rename");
+ }else if (cparse_cplusplus)
+ $$ = 0; /* ignore unnamed structs for C++ */
+ Delete(unnamed);
+ } else { /* unnamed struct w/o declarator*/
+ Swig_symbol_popscope();
+ Delete(Namespaceprefix);
+ Namespaceprefix = Swig_symbol_qualifiedscopename(0);
+ add_symbols($6);
+ Delete($$);
+ $$ = $6; /* pass member list to outer class/namespace (instead of self)*/
}
- Delete(Namespaceprefix);
- Namespaceprefix = Swig_symbol_qualifiedscopename(0);
- add_symbols($$);
- add_symbols(n);
- Delete(unnamed);
}
;
@@ -4107,41 +3746,10 @@ cpp_forward_class_decl : storage_class cpptype idcolon SEMI {
------------------------------------------------------------ */
cpp_template_decl : TEMPLATE LESSTHAN template_parms GREATERTHAN {
+ if (currentOuterClass)
+ Setattr(currentOuterClass, "template_parameters", template_parameters);
template_parameters = $3;
- if (inclass)
- nested_template++;
-
} cpp_temp_possible {
-
- /* Don't ignore templated functions declared within a class, unless the templated function is within a nested class */
- if (nested_template <= 1) {
- int is_nested_template_class = $6 && GetFlag($6, "nestedtemplateclass");
- if (is_nested_template_class) {
- $$ = 0;
- /* Nested template classes would probably better be ignored like ordinary nested classes using cpp_nested, but that introduces shift/reduce conflicts */
- if (cplus_mode == CPLUS_PUBLIC) {
- /* Treat the nested class/struct/union as a forward declaration until a proper nested class solution is implemented */
- String *kind = Getattr($6, "kind");
- String *name = Getattr($6, "name");
- $$ = new_node("template");
- Setattr($$,"kind",kind);
- Setattr($$,"name",name);
- Setattr($$,"sym:weak", "1");
- Setattr($$,"templatetype","classforward");
- Setattr($$,"templateparms", $3);
- add_symbols($$);
-
- if (GetFlag($$, "feature:nestedworkaround")) {
- Swig_symbol_remove($$);
- $$ = 0;
- } else {
- SWIG_WARN_NODE_BEGIN($$);
- Swig_warning(WARN_PARSE_NAMED_NESTED_CLASS, cparse_file, cparse_line, "Nested template %s not currently supported (%s ignored).\n", kind, name);
- SWIG_WARN_NODE_END($$);
- }
- }
- Delete($6);
- } else {
String *tname = 0;
int error = 0;
@@ -4387,13 +3995,10 @@ cpp_template_decl : TEMPLATE LESSTHAN template_parms GREATERTHAN {
Delete(Namespaceprefix);
Namespaceprefix = Swig_symbol_qualifiedscopename(0);
if (error) $$ = 0;
- }
- } else {
- $$ = 0;
- }
- template_parameters = 0;
- if (inclass)
- nested_template--;
+ if (currentOuterClass)
+ template_parameters = Getattr(currentOuterClass, "template_parameters");
+ else
+ template_parameters = 0;
}
/* Explicit template instantiation */
@@ -4622,6 +4227,8 @@ cpp_members : cpp_member cpp_members {
p = nextSibling(p);
}
set_nextSibling(pp,$2);
+ if ($2)
+ set_previousSibling($2, pp);
} else {
$$ = $2;
}
@@ -4678,7 +4285,8 @@ cpp_member : c_declaration { $$ = $1; }
| cpp_swig_directive { $$ = $1; }
| cpp_conversion_operator { $$ = $1; }
| cpp_forward_class_decl { $$ = $1; }
- | cpp_nested { $$ = $1; }
+ | cpp_class_decl { $$ = $1; }
+/* | cpp_nested { $$ = $1; }*/
| storage_class idcolon SEMI { $$ = 0; }
| cpp_using_decl { $$ = $1; }
| cpp_template_decl { $$ = $1; }
@@ -4906,82 +4514,6 @@ cpp_protection_decl : PUBLIC COLON {
cplus_mode = CPLUS_PROTECTED;
}
;
-
-
-/* ------------------------------------------------------------
- Named nested structs:
- struct sname { };
- struct sname { } id;
- struct sname : bases { };
- struct sname : bases { } id;
- typedef sname struct { } td;
- typedef sname struct : bases { } td;
-
- Adding inheritance, ie replacing 'ID' with 'idcolon inherit'
- added one shift/reduce
- ------------------------------------------------------------ */
-
-cpp_nested : storage_class cpptype idcolon inherit LBRACE {
- cparse_start_line = cparse_line;
- skip_balanced('{','}');
- $<str>$ = NewString(scanner_ccode); /* copied as initializers overwrite scanner_ccode */
- } cpp_opt_declarators {
- $$ = 0;
- if (cplus_mode == CPLUS_PUBLIC) {
- if (cparse_cplusplus) {
- String *name = Copy($3);
- $$ = nested_forward_declaration($1, $2, $3, name, $7);
- } else if ($7) {
- nested_new_struct($2, $<str>6, $7);
- }
- }
- Delete($<str>6);
- }
-
-/* ------------------------------------------------------------
- Unnamed/anonymous nested structs:
- struct { };
- struct { } id;
- struct : bases { };
- struct : bases { } id;
- typedef struct { } td;
- typedef struct : bases { } td;
- ------------------------------------------------------------ */
-
- | storage_class cpptype inherit LBRACE {
- cparse_start_line = cparse_line;
- skip_balanced('{','}');
- $<str>$ = NewString(scanner_ccode); /* copied as initializers overwrite scanner_ccode */
- } cpp_opt_declarators {
- $$ = 0;
- if (cplus_mode == CPLUS_PUBLIC) {
- if (cparse_cplusplus) {
- String *name = $6 ? Copy(Getattr($6, "name")) : 0;
- $$ = nested_forward_declaration($1, $2, 0, name, $6);
- } else {
- if ($6) {
- nested_new_struct($2, $<str>5, $6);
- } else {
- Swig_warning(WARN_PARSE_UNNAMED_NESTED_CLASS, cparse_file, cparse_line, "Nested %s not currently supported (ignored).\n", $2);
- }
- }
- }
- Delete($<str>5);
- }
-
-
-/* This unfortunately introduces 4 shift/reduce conflicts, so instead the somewhat hacky nested_template is used for ignore nested template classes. */
-/*
- | TEMPLATE LESSTHAN template_parms GREATERTHAN cpptype idcolon LBRACE { cparse_start_line = cparse_line; skip_balanced('{','}');
- } SEMI {
- $$ = 0;
- if (cplus_mode == CPLUS_PUBLIC) {
- Swig_warning(WARN_PARSE_NAMED_NESTED_CLASS, cparse_file, cparse_line,"Nested %s not currently supported (%s ignored)\n", $5, $6);
- }
- }
-*/
- ;
-
/* These directives can be included inside a class definition */
cpp_swig_directive: pragma_directive { $$ = $1; }
View
2  Source/Include/swigwarn.h
@@ -75,7 +75,6 @@
#define WARN_PARSE_PRIVATE_INHERIT 309
#define WARN_PARSE_TEMPLATE_REPEAT 310
#define WARN_PARSE_TEMPLATE_PARTIAL 311
-#define WARN_PARSE_UNNAMED_NESTED_CLASS 312
#define WARN_PARSE_UNDEFINED_EXTERN 313
#define WARN_PARSE_KEYWORD 314
#define WARN_PARSE_USING_UNDEF 315
@@ -88,7 +87,6 @@
#define WARN_PARSE_REDUNDANT 322
#define WARN_PARSE_REC_INHERITANCE 323
#define WARN_PARSE_NESTED_TEMPLATE 324
-#define WARN_PARSE_NAMED_NESTED_CLASS 325
#define WARN_PARSE_EXTEND_NAME 326
#define WARN_CPP11_LAMBDA 340
View
6 Source/Modules/allocate.cxx
@@ -559,6 +559,8 @@ Allocate():
virtual int classDeclaration(Node *n) {
Symtab *symtab = Swig_symbol_current();
Swig_symbol_setscope(Getattr(n, "symtab"));
+ Node* oldInclass = inclass;
+ AccessMode oldAcessMode = cplus_mode;
if (!CPlusPlus) {
/* Always have default constructors/destructors in C */
@@ -580,7 +582,6 @@ Allocate():
}
}
}
-
inclass = n;
String *kind = Getattr(n, "kind");
if (Strcmp(kind, "class") == 0) {
@@ -728,7 +729,8 @@ Allocate():
/* Only care about default behavior. Remove temporary values */
Setattr(n, "allocate:visit", "1");
- inclass = 0;
+ inclass = oldInclass;
+ cplus_mode = oldAcessMode;
Swig_symbol_setscope(symtab);
return SWIG_OK;
}
View
6 Source/Modules/contract.cxx
@@ -342,11 +342,13 @@ int Contracts::namespaceDeclaration(Node *n) {
int Contracts::classDeclaration(Node *n) {
int ret = SWIG_OK;
+ int oldInClass = InClass;
+ Node* oldClass = CurrentClass;
InClass = 1;
CurrentClass = n;
emit_children(n);
- InClass = 0;
- CurrentClass = 0;
+ InClass = oldInClass;
+ CurrentClass = oldClass;
return ret;
}
View
231 Source/Modules/csharp.cxx
@@ -18,6 +18,8 @@
/* Hash type used for upcalls from C/C++ */
typedef DOH UpcallData;
+// insert N tabs before each new line in s
+void Swig_offset_string(String* s, int N);
class CSHARP:public Language {
static const char *usage;
@@ -53,7 +55,6 @@ class CSHARP:public Language {
String *proxy_class_code;
String *module_class_code;
String *proxy_class_name; // proxy class name
- String *full_proxy_class_name;// fully qualified proxy class name when using nspace feature, otherwise same as proxy_class_name
String *full_imclass_name; // fully qualified intermediary class name when using nspace feature, otherwise same as imclass_name
String *variable_name; //Name of a variable being wrapped
String *proxy_class_constants_code;
@@ -87,6 +88,7 @@ class CSHARP:public Language {
int n_directors;
int first_class_dmethod;
int curr_class_dmethod;
+ int nesting_depth;
enum EnumFeature { SimpleEnum, TypeunsafeEnum, TypesafeEnum, ProperEnum };
@@ -125,7 +127,6 @@ class CSHARP:public Language {
proxy_class_code(NULL),
module_class_code(NULL),
proxy_class_name(NULL),
- full_proxy_class_name(NULL),
full_imclass_name(NULL),
variable_name(NULL),
proxy_class_constants_code(NULL),
@@ -156,7 +157,8 @@ class CSHARP:public Language {
n_dmethods(0),
n_directors(0),
first_class_dmethod(0),
- curr_class_dmethod(0) {
+ curr_class_dmethod(0),
+ nesting_depth(0){
/* for now, multiple inheritance in directors is disabled, this
should be easy to implement though */
director_multiple_inheritance = 0;
@@ -179,7 +181,13 @@ class CSHARP:public Language {
proxyname = Getattr(n, "proxyname");
if (!proxyname) {
String *nspace = Getattr(n, "sym:nspace");
- String *symname = Getattr(n, "sym:name");
+ String *symname = Copy(Getattr(n, "sym:name"));
+ if (!GetFlag(n, "feature:flatnested")) {
+ for (Node* outer_class = Getattr(n, "nested:outer");outer_class;outer_class = Getattr(outer_class, "nested:outer")) {
+ Push(symname, ".");
+ Push(symname, Getattr(outer_class, "sym:name"));
+ }
+ }
if (nspace) {
if (namespce)
proxyname = NewStringf("%s.%s.%s", namespce, nspace, symname);
@@ -190,12 +198,33 @@ class CSHARP:public Language {
}
Setattr(n, "proxyname", proxyname);
Delete(proxyname);
+ Delete(symname);
}
}
}
return proxyname;
}
+ /* -----------------------------------------------------------------------------
+ * directorClassName()
+ * ----------------------------------------------------------------------------- */
+
+ String *directorClassName(Node *n) {
+ String *dirclassname;
+ const char *attrib = "director:classname";
+
+ if (!(dirclassname = Getattr(n, attrib))) {
+ String *classname = getClassPrefix();
+
+ dirclassname = NewStringf("SwigDirector_%s", classname);
+ Setattr(n, attrib, dirclassname);
+ }
+ else
+ dirclassname = Copy(dirclassname);
+
+ return dirclassname;
+ }
+
/* ------------------------------------------------------------
* main()
* ------------------------------------------------------------ */
@@ -1025,7 +1054,7 @@ class CSHARP:public Language {
*/
if (proxy_flag && wrapping_member_flag && !enum_constant_flag) {
// Capitalize the first letter in the variable in the getter/setter function name
- bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, proxy_class_name, variable_name))) != 0;
+ bool getter_flag = Cmp(symname, Swig_name_set(getNSpace(), Swig_name_member(0, getClassPrefix(), variable_name))) != 0;
String *getter_setter_name = NewString("");
if (!getter_flag)
@@ -1589,6 +1618,7 @@ class CSHARP:public Language {
String *c_baseclassname = NULL;
SwigType *typemap_lookup_type = Getattr(n, "classtypeobj");
bool feature_director = Swig_directorclass(n) ? true : false;
+ bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested");
// Inheritance from pure C# classes
Node *attributes = NewHash();
@@ -1648,7 +1678,8 @@ class CSHARP:public Language {
// Pure C# interfaces
const String *pure_interfaces = typemapLookup(n, derived ? "csinterfaces_derived" : "csinterfaces", typemap_lookup_type, WARN_NONE);
// Start writing the proxy class
- Printv(proxy_class_def, typemapLookup(n, "csimports", typemap_lookup_type, WARN_NONE), // Import statements
+ if (!has_outerclass)
+ Printv(proxy_class_def, typemapLookup(n, "csimports", typemap_lookup_type, WARN_NONE), // Import statements
"\n", NIL);
// Class attributes
@@ -1719,7 +1750,7 @@ class CSHARP:public Language {
Printf(proxy_class_code, " if (SwigDerivedClassHasMethod(\"%s\", swigMethodTypes%s))\n", method, methid);
Printf(proxy_class_code, " swigDelegate%s = new SwigDelegate%s_%s(SwigDirector%s);\n", methid, proxy_class_name, methid, overname);
}
- String *director_connect_method_name = Swig_name_member(getNSpace(), proxy_class_name, "director_connect");
+ String *director_connect_method_name = Swig_name_member(getNSpace(), getClassPrefix(), "director_connect");
Printf(proxy_class_code, " %s.%s(swigCPtr", imclass_name, director_connect_method_name);
for (i = first_class_dmethod; i < curr_class_dmethod; ++i) {
UpcallData *udata = Getitem(dmethods_seq, i);
@@ -1795,7 +1826,7 @@ class CSHARP:public Language {
// Add code to do C++ casting to base class (only for classes in an inheritance hierarchy)
if (derived) {
String *smartptr = Getattr(n, "feature:smartptr");
- String *upcast_method = Swig_name_member(getNSpace(), proxy_class_name, smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
+ String *upcast_method = Swig_name_member(getNSpace(), getClassPrefix(), smartptr != 0 ? "SWIGSmartPtrUpcast" : "SWIGUpcast");
String *wname = Swig_name_wrapper(upcast_method);
Printv(imclass_cppcasts_code, "\n [global::System.Runtime.InteropServices.DllImport(\"", dllimport, "\", EntryPoint=\"", wname, "\")]\n", NIL);
@@ -1846,11 +1877,35 @@ class CSHARP:public Language {
String *nspace = getNSpace();
File *f_proxy = NULL;
+ // save class local variables
+ String* old_proxy_class_name = proxy_class_name;
+ String* old_full_imclass_name = full_imclass_name;
+ String* old_destructor_call = destructor_call;
+ String* old_proxy_class_constants_code = proxy_class_constants_code;
+ String* old_proxy_class_def = proxy_class_def;
+ String* old_proxy_class_code = proxy_class_code;
+
if (proxy_flag) {
proxy_class_name = NewString(Getattr(n, "sym:name"));
+ if (Node* outer = Getattr(n, "nested:outer")) {
+ String* outerClassesPrefix = Copy(Getattr(outer, "sym:name"));
+ for (outer = Getattr(outer, "nested:outer"); outer != 0; outer = Getattr(outer, "nested:outer")) {
+ Push(outerClassesPrefix, "::");
+ Push(outerClassesPrefix, Getattr(outer, "sym:name"));
+ }
+ String* fnspace = nspace ? NewStringf("%s::%s", nspace, outerClassesPrefix) : outerClassesPrefix;
+ if (!addSymbol(proxy_class_name, n, fnspace))
+ return SWIG_ERROR;
+ if (nspace)
+ Delete(fnspace);
+ Delete(outerClassesPrefix);
+ }
+ else {
+ if (!addSymbol(proxy_class_name, n, nspace))
+ return SWIG_ERROR;
+ }
if (!nspace) {
- full_proxy_class_name = NewStringf("%s", proxy_class_name);
full_imclass_name = NewStringf("%s", imclass_name);
if (Cmp(proxy_class_name, imclass_name) == 0) {
Printf(stderr, "Class name cannot be equal to intermediary class name: %s\n", proxy_class_name);
@@ -1863,36 +1918,34 @@ class CSHARP:public Language {
}
} else {
if (namespce) {
- full_proxy_class_name = NewStringf("%s.%s.%s", namespce, nspace, proxy_class_name);
full_imclass_name = NewStringf("%s.%s", namespce, imclass_name);
} else {
- full_proxy_class_name = NewStringf("%s.%s", nspace, proxy_class_name);
full_imclass_name = NewStringf("%s", imclass_name);
}
}
- if (!addSymbol(proxy_class_name, n, nspace))
- return SWIG_ERROR;
-
- String *output_directory = outputDirectory(nspace);
- String *filen = NewStringf("%s%s.cs", output_directory, proxy_class_name);
- f_proxy = NewFile(filen, "w", SWIG_output_files());
- if (!f_proxy) {
- FileErrorDisplay(filen);
- SWIG_exit(EXIT_FAILURE);
- }
- Append(filenames_list, Copy(filen));
- Delete(filen);
- filen = NULL;
-
- // Start writing out the proxy class file
- emitBanner(f_proxy);
-
- addOpenNamespace(nspace, f_proxy);
+ // inner class doesn't need this prologue
+ if (!Getattr(n, "nested:outer")) {
+ String *output_directory = outputDirectory(nspace);
+ String *filen = NewStringf("%s%s.cs", output_directory, proxy_class_name);
+ f_proxy = NewFile(filen, "w", SWIG_output_files());
+ if (!f_proxy) {
+ FileErrorDisplay(filen);
+ SWIG_exit(EXIT_FAILURE);
+ }
+ Append(filenames_list, Copy(filen));
+ Delete(filen);
+ filen = NULL;
- Clear(proxy_class_def);
- Clear(proxy_class_code);
+ // Start writing out the proxy class file
+ emitBanner(f_proxy);
+ addOpenNamespace(nspace, f_proxy);
+ }
+ else
+ ++nesting_depth;
+ proxy_class_def = NewString("");
+ proxy_class_code = NewString("");
destructor_call = NewString("");
proxy_class_constants_code = NewString("");
}
@@ -1903,7 +1956,7 @@ class CSHARP:public Language {
emitProxyClassDefAndCPPCasts(n);
- String *csclazzname = Swig_name_member(getNSpace(), proxy_class_name, ""); // mangled full proxy class name
+ String *csclazzname = Swig_name_member(getNSpace(), getClassPrefix(), ""); // mangled full proxy class name
Replaceall(proxy_class_def, "$csclassname", proxy_class_name);
Replaceall(proxy_class_code, "$csclassname", proxy_class_name);
@@ -1924,17 +1977,36 @@ class CSHARP:public Language {
Replaceall(proxy_class_def, "$dllimport", dllimport);
Replaceall(proxy_class_code, "$dllimport", dllimport);
Replaceall(proxy_class_constants_code, "$dllimport", dllimport);
-
- Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);
+ bool has_outerclass = Getattr(n, "nested:outer") != 0 && !GetFlag(n, "feature:flatnested");
+ if (!has_outerclass)
+ Printv(f_proxy, proxy_class_def, proxy_class_code, NIL);
+ else {
+ Swig_offset_string(proxy_class_def, nesting_depth);
+ Append(old_proxy_class_code, proxy_class_def);
+ Swig_offset_string(proxy_class_code, nesting_depth);
+ Append(old_proxy_class_code, proxy_class_code);
+ }
// Write out all the constants
- if (Len(proxy_class_constants_code) != 0)
- Printv(f_proxy, proxy_class_constants_code, NIL);
-
- Printf(f_proxy, "}\n");
- addCloseNamespace(nspace, f_proxy);
- Delete(f_proxy);
- f_proxy = NULL;
+ if (Len(proxy_class_constants_code) != 0) {
+ if (!has_outerclass)
+ Printv(f_proxy, proxy_class_constants_code, NIL);
+ else {