Browse Source

Fixed generation of nested declarations in classes.

pull/68/merge
triton 13 years ago
parent
commit
c7b7e2c703
  1. 77
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 1
      tests/Basic/Basic.Tests.cs
  3. 5
      tests/Basic/Basic.cpp
  4. 7
      tests/Basic/Basic.h

77
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -91,27 +91,10 @@ namespace CppSharp.Generators.CLI @@ -91,27 +91,10 @@ namespace CppSharp.Generators.CLI
WriteLine(forwardRef);
}
public void GenerateNamespace(Namespace @namespace)
public void GenerateDeclContext(DeclarationContext decl)
{
var isTopLevel = @namespace is TranslationUnit;
var generateNamespace = !isTopLevel || Options.GenerateLibraryNamespace;
if (generateNamespace)
{
PushBlock(CLIBlockKind.Namespace, @namespace);
WriteLine("namespace {0}", isTopLevel
? Options.OutputNamespace
: SafeIdentifier(@namespace.Name));
WriteStartBraceIndent();
}
// Generate the forward references.
PushBlock(CLIBlockKind.ForwardReferences);
GenerateForwardRefs(@namespace);
PopBlock(NewLineKind.BeforeNextBlock);
// Generate all the enum declarations for the module.
foreach (var @enum in @namespace.Enums)
foreach (var @enum in decl.Enums)
{
if (@enum.Ignore || @enum.IsIncomplete)
continue;
@ -122,10 +105,10 @@ namespace CppSharp.Generators.CLI @@ -122,10 +105,10 @@ namespace CppSharp.Generators.CLI
}
// Generate all the typedef declarations for the module.
GenerateTypedefs(@namespace);
GenerateTypedefs(decl);
// Generate all the struct/class declarations for the module.
foreach (var @class in @namespace.Classes)
foreach (var @class in decl.Classes)
{
if (@class.Ignore || @class.IsIncomplete)
continue;
@ -138,11 +121,33 @@ namespace CppSharp.Generators.CLI @@ -138,11 +121,33 @@ namespace CppSharp.Generators.CLI
PopBlock(NewLineKind.BeforeNextBlock);
}
if (@namespace.HasFunctions)
GenerateFunctions(@namespace);
if (decl.HasFunctions)
GenerateFunctions(decl);
foreach(var childNamespace in @namespace.Namespaces)
foreach (var childNamespace in decl.Namespaces)
GenerateNamespace(childNamespace);
}
public void GenerateNamespace(Namespace @namespace)
{
var isTopLevel = @namespace is TranslationUnit;
var generateNamespace = !isTopLevel || Options.GenerateLibraryNamespace;
if (generateNamespace)
{
PushBlock(CLIBlockKind.Namespace, @namespace);
WriteLine("namespace {0}", isTopLevel
? Options.OutputNamespace
: SafeIdentifier(@namespace.Name));
WriteStartBraceIndent();
}
// Generate the forward references.
PushBlock(CLIBlockKind.ForwardReferences);
GenerateForwardRefs(@namespace);
PopBlock(NewLineKind.BeforeNextBlock);
GenerateDeclContext(@namespace);
if (generateNamespace)
{
@ -151,9 +156,9 @@ namespace CppSharp.Generators.CLI @@ -151,9 +156,9 @@ namespace CppSharp.Generators.CLI
}
}
public void GenerateTypedefs(Namespace @namespace)
public void GenerateTypedefs(DeclarationContext decl)
{
foreach (var typedef in @namespace.Typedefs)
foreach (var typedef in decl.Typedefs)
{
if (typedef.Ignore)
continue;
@ -162,7 +167,7 @@ namespace CppSharp.Generators.CLI @@ -162,7 +167,7 @@ namespace CppSharp.Generators.CLI
}
}
public void GenerateFunctions(Namespace @namespace)
public void GenerateFunctions(DeclarationContext decl)
{
PushBlock(CLIBlockKind.FunctionsClass);
@ -173,7 +178,7 @@ namespace CppSharp.Generators.CLI @@ -173,7 +178,7 @@ namespace CppSharp.Generators.CLI
PushIndent();
// Generate all the function declarations for the module.
foreach (var function in @namespace.Functions)
foreach (var function in decl.Functions)
{
GenerateFunction(function);
}
@ -194,12 +199,10 @@ namespace CppSharp.Generators.CLI @@ -194,12 +199,10 @@ namespace CppSharp.Generators.CLI
if (GenerateClassProlog(@class))
return;
foreach (var @enum in @class.Enums)
{
PushIndent();
GenerateEnum(@enum);
PopIndent();
}
// Process the nested types.
PushIndent();
GenerateDeclContext(@class);
PopIndent();
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
@ -453,6 +456,12 @@ namespace CppSharp.Generators.CLI @@ -453,6 +456,12 @@ namespace CppSharp.Generators.CLI
{
Write("public ");
// Nested types cannot have visibility modifiers in C++/CLI.
var isTopLevel = @class.Namespace is TranslationUnit ||
@class.Namespace is Namespace;
if (isTopLevel)
Write("public ");
Write(@class.IsValueType ? "value struct " : "ref class ");
Write("{0}", SafeIdentifier(@class.Name));

1
tests/Basic/Basic.Tests.cs

@ -24,6 +24,7 @@ public class BasicTests @@ -24,6 +24,7 @@ public class BasicTests
var bar = new Bar { A = 4, B = 7 };
Assert.That(hello.AddBar(bar), Is.EqualTo(11));
Assert.That(bar.RetItem1(), Is.EqualTo(Bar.Item.Item1));
var retFoo = hello.RetFoo(7, 2.0f);
Assert.That(retFoo.A, Is.EqualTo(7));

5
tests/Basic/Basic.cpp

@ -27,6 +27,11 @@ Bar::Bar() @@ -27,6 +27,11 @@ Bar::Bar()
{
}
Bar::Item Bar::RetItem1()
{
return Bar::Item::Item1;
}
Hello::Hello ()
{
//cout << "Ctor!" << "\n";

7
tests/Basic/Basic.h

@ -33,7 +33,14 @@ public: @@ -33,7 +33,14 @@ public:
struct DLL_API Bar
{
enum class Item
{
Item1,
Item2
};
Bar();
Item RetItem1();
int A;
float B;
};

Loading…
Cancel
Save