From c7b7e2c703ce239752042ef3a2e4c746c6cfbf6e Mon Sep 17 00:00:00 2001 From: triton Date: Fri, 20 Sep 2013 01:17:57 +0100 Subject: [PATCH] Fixed generation of nested declarations in classes. --- .../Generators/CLI/CLIHeadersTemplate.cs | 77 +++++++++++-------- tests/Basic/Basic.Tests.cs | 1 + tests/Basic/Basic.cpp | 5 ++ tests/Basic/Basic.h | 7 ++ 4 files changed, 56 insertions(+), 34 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 887d3861..d42a0db8 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -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 } // 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 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 } } - 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 } } - public void GenerateFunctions(Namespace @namespace) + public void GenerateFunctions(DeclarationContext decl) { PushBlock(CLIBlockKind.FunctionsClass); @@ -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 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 { 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)); diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index 395b8bb1..bcd8639e 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -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)); diff --git a/tests/Basic/Basic.cpp b/tests/Basic/Basic.cpp index 97139072..ba16bcd2 100644 --- a/tests/Basic/Basic.cpp +++ b/tests/Basic/Basic.cpp @@ -27,6 +27,11 @@ Bar::Bar() { } +Bar::Item Bar::RetItem1() +{ + return Bar::Item::Item1; +} + Hello::Hello () { //cout << "Ctor!" << "\n"; diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index 57b0dbb2..6b2d4a66 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -33,7 +33,14 @@ public: struct DLL_API Bar { + enum class Item + { + Item1, + Item2 + }; + Bar(); + Item RetItem1(); int A; float B; };