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
WriteLine(forwardRef); 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. // 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) if (@enum.Ignore || @enum.IsIncomplete)
continue; continue;
@ -122,10 +105,10 @@ namespace CppSharp.Generators.CLI
} }
// Generate all the typedef declarations for the module. // Generate all the typedef declarations for the module.
GenerateTypedefs(@namespace); GenerateTypedefs(decl);
// Generate all the struct/class declarations for the module. // 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) if (@class.Ignore || @class.IsIncomplete)
continue; continue;
@ -138,11 +121,33 @@ namespace CppSharp.Generators.CLI
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
} }
if (@namespace.HasFunctions) if (decl.HasFunctions)
GenerateFunctions(@namespace); GenerateFunctions(decl);
foreach(var childNamespace in @namespace.Namespaces) foreach (var childNamespace in decl.Namespaces)
GenerateNamespace(childNamespace); 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) if (generateNamespace)
{ {
@ -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) if (typedef.Ignore)
continue; continue;
@ -162,7 +167,7 @@ namespace CppSharp.Generators.CLI
} }
} }
public void GenerateFunctions(Namespace @namespace) public void GenerateFunctions(DeclarationContext decl)
{ {
PushBlock(CLIBlockKind.FunctionsClass); PushBlock(CLIBlockKind.FunctionsClass);
@ -173,7 +178,7 @@ namespace CppSharp.Generators.CLI
PushIndent(); PushIndent();
// Generate all the function declarations for the module. // Generate all the function declarations for the module.
foreach (var function in @namespace.Functions) foreach (var function in decl.Functions)
{ {
GenerateFunction(function); GenerateFunction(function);
} }
@ -194,12 +199,10 @@ namespace CppSharp.Generators.CLI
if (GenerateClassProlog(@class)) if (GenerateClassProlog(@class))
return; return;
foreach (var @enum in @class.Enums) // Process the nested types.
{ PushIndent();
PushIndent(); GenerateDeclContext(@class);
GenerateEnum(@enum); PopIndent();
PopIndent();
}
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
@ -453,6 +456,12 @@ namespace CppSharp.Generators.CLI
{ {
Write("public "); 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(@class.IsValueType ? "value struct " : "ref class ");
Write("{0}", SafeIdentifier(@class.Name)); Write("{0}", SafeIdentifier(@class.Name));

1
tests/Basic/Basic.Tests.cs

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

5
tests/Basic/Basic.cpp

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

7
tests/Basic/Basic.h

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

Loading…
Cancel
Save