From b208d7b58879f3c6fc59c312ffb57fee8f194d39 Mon Sep 17 00:00:00 2001 From: triton Date: Thu, 14 Mar 2013 16:01:25 +0000 Subject: [PATCH] Rework the CLI generator to actually handle (nested) namespaces propertly. --- .../Generators/CLI/CLIHeadersTemplate.cs | 67 ++++++++++--------- src/Generator/Generators/CLI/CLIMarshal.cs | 10 ++- .../Generators/CLI/CLISourcesTemplate.cs | 32 +++++---- .../Generators/CLI/CLITextTemplate.cs | 4 +- .../Generators/CLI/CLITypePrinter.cs | 2 +- 5 files changed, 68 insertions(+), 47 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 032a0bb4..f2097dc9 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -28,10 +28,7 @@ namespace Cxxi.Generators.CLI NewLine(); - WriteLine("namespace {0}", SafeIdentifier(Library.Name)); - WriteLine("{"); - GenerateDeclarations(); - WriteLine("}"); + GenerateNamespace(TranslationUnit); } public void GenerateIncludeForwardRefs() @@ -61,7 +58,7 @@ namespace Cxxi.Generators.CLI WriteLine(include); } - public void GenerateForwardRefs() + public void GenerateForwardRefs(Namespace @namespace) { var typeRefs = TranslationUnit.TypeReferences as TypeRefsVisitor; @@ -88,40 +85,46 @@ namespace Cxxi.Generators.CLI NewLine(); } - public void GenerateDeclarations() + public void GenerateNamespace(Namespace @namespace) { - PushIndent(); + var isTopLevel = @namespace is TranslationUnit; + var generateNamespace = !isTopLevel || Options.GenerateLibraryNamespace; - // Generate the forward references. - GenerateForwardRefs(); + if (generateNamespace) + { + WriteLine("namespace {0}", isTopLevel + ? Options.OutputNamespace + : SafeIdentifier(@namespace.Name)); + WriteStartBraceIndent(); + } - bool needsNewline = false; + // Generate the forward references. + GenerateForwardRefs(@namespace); // Generate all the enum declarations for the module. - for (var i = 0; i < TranslationUnit.Enums.Count; ++i) + for (var i = 0; i < @namespace.Enums.Count; ++i) { - var @enum = TranslationUnit.Enums[i]; + var @enum = @namespace.Enums[i]; if (@enum.Ignore || @enum.IsIncomplete) continue; GenerateEnum(@enum); NeedNewLine(); - if (i < TranslationUnit.Enums.Count - 1) + + if (i < @namespace.Enums.Count - 1) NewLine(); } NewLineIfNeeded(); // Generate all the typedef declarations for the module. - GenerateTypedefs(); - - needsNewline = false; + GenerateTypedefs(@namespace); // Generate all the struct/class declarations for the module. - for (var i = 0; i < TranslationUnit.Classes.Count; ++i) + for (var i = 0; i < @namespace.Classes.Count; ++i) { - var @class = TranslationUnit.Classes[i]; + var @class = @namespace.Classes[i]; if (@class.Ignore || @class.IsIncomplete) continue; @@ -130,26 +133,30 @@ namespace Cxxi.Generators.CLI continue; GenerateClass(@class); - needsNewline = true; + NeedNewLine(); - if (i < TranslationUnit.Classes.Count - 1) + if (i < @namespace.Classes.Count - 1) NewLine(); } - if (TranslationUnit.HasFunctions) + if (@namespace.HasFunctions) { - if (needsNewline) - NewLine(); - - GenerateFunctions(); + NewLineIfNeeded(); + GenerateFunctions(@namespace); } - PopIndent(); + foreach(var childNamespace in @namespace.Namespaces) + GenerateNamespace(childNamespace); + + if (generateNamespace) + { + WriteCloseBraceIndent(); + } } - public void GenerateTypedefs() + public void GenerateTypedefs(Namespace @namespace) { - foreach (var typedef in TranslationUnit.Typedefs) + foreach (var typedef in @namespace.Typedefs) { if (typedef.Ignore) continue; @@ -161,7 +168,7 @@ namespace Cxxi.Generators.CLI } } - public void GenerateFunctions() + public void GenerateFunctions(Namespace @namespace) { WriteLine("public ref class {0}{1}", SafeIdentifier(Library.Name), TranslationUnit.FileNameWithoutExtension); @@ -170,7 +177,7 @@ namespace Cxxi.Generators.CLI PushIndent(); // Generate all the function declarations for the module. - foreach (var function in TranslationUnit.Functions) + foreach (var function in @namespace.Functions) { GenerateFunction(function); } diff --git a/src/Generator/Generators/CLI/CLIMarshal.cs b/src/Generator/Generators/CLI/CLIMarshal.cs index 23c2906d..7ffe1beb 100644 --- a/src/Generator/Generators/CLI/CLIMarshal.cs +++ b/src/Generator/Generators/CLI/CLIMarshal.cs @@ -182,12 +182,20 @@ namespace Cxxi.Generators.CLI return true; } + public string QualifiedIdentifier(Declaration decl) + { + if (Driver.Options.GenerateLibraryNamespace) + return string.Format("{0}::{1}", Driver.Options.OutputNamespace, + decl.QualifiedName); + return string.Format("{0}", decl.QualifiedName); + } + public void WriteClassInstance(Class @class, string instance) { if (@class.IsRefType) Return.Write("gcnew "); - Return.Write("{0}::{1}(", Library.Name, @class.Name); + Return.Write("{0}(", QualifiedIdentifier(@class)); Return.Write("(::{0}*)", @class.QualifiedOriginalName); Return.Write("{0})", instance); } diff --git a/src/Generator/Generators/CLI/CLISourcesTemplate.cs b/src/Generator/Generators/CLI/CLISourcesTemplate.cs index 7b14f98d..188fd17d 100644 --- a/src/Generator/Generators/CLI/CLISourcesTemplate.cs +++ b/src/Generator/Generators/CLI/CLISourcesTemplate.cs @@ -74,11 +74,13 @@ namespace Cxxi.Generators.CLI public void GenerateDeclarations() { - // Generate all the struct/class definitions for the module. - for (var i = 0; i < TranslationUnit.Classes.Count; ++i) - { - var @class = TranslationUnit.Classes[i]; + GenerateNamespace(TranslationUnit); + } + private void GenerateNamespace(Namespace @namespace) + { + foreach (var @class in @namespace.Classes) + { if (@class.Ignore) continue; @@ -88,22 +90,21 @@ namespace Cxxi.Generators.CLI GenerateClass(@class); } - if (TranslationUnit.HasFunctions) + if (@namespace.HasFunctions) { - var staticClassName = Library.Name + TranslationUnit.FileNameWithoutExtension; - // Generate all the function declarations for the module. - for (var i = 0; i < TranslationUnit.Functions.Count; ++i) + foreach (var function in @namespace.Functions) { - var function = TranslationUnit.Functions[i]; - if (function.Ignore) continue; - GenerateFunction(function, staticClassName); + GenerateFunction(function, @namespace); NewLine(); } } + + foreach(var childNamespace in @namespace.Namespaces) + GenerateNamespace(childNamespace); } public void GenerateDeclarationCommon(Declaration decl) @@ -254,7 +255,7 @@ namespace Cxxi.Generators.CLI { Write("{0}::{1}(", QualifiedIdentifier(@class), SafeIdentifier(@class.Name)); - var nativeType = string.Format("::{0}*", @class.OriginalName); + var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); WriteLine("{0} native)", isIntPtr ? "System::IntPtr" : nativeType); var hasBase = GenerateClassConstructorBase(@class); @@ -372,14 +373,17 @@ namespace Cxxi.Generators.CLI WriteCloseBraceIndent(); } - public void GenerateFunction(Function function, string className) + public void GenerateFunction(Function function, Namespace @namespace) { if (function.Ignore) return; GenerateDeclarationCommon(function); - Write("{0} {1}::{2}::{3}(", function.ReturnType, Library.Name, className, + var classSig = string.Format("{0}::{1}{2}", QualifiedIdentifier(@namespace), + Library.Name, TranslationUnit.FileNameWithoutExtension); + + Write("{0} {1}::{2}(", function.ReturnType, classSig, SafeIdentifier(function.Name)); for (var i = 0; i < function.Parameters.Count; ++i) diff --git a/src/Generator/Generators/CLI/CLITextTemplate.cs b/src/Generator/Generators/CLI/CLITextTemplate.cs index 3b1df24d..0817c626 100644 --- a/src/Generator/Generators/CLI/CLITextTemplate.cs +++ b/src/Generator/Generators/CLI/CLITextTemplate.cs @@ -45,7 +45,9 @@ public abstract class CLITextTemplate : TextTemplate public string QualifiedIdentifier(Declaration decl) { - return string.Format("{0}::{1}", Library.Name, decl.Name); + if (Options.GenerateLibraryNamespace) + return string.Format("{0}::{1}", Options.OutputNamespace, decl.QualifiedName); + return string.Format("{0}", decl.QualifiedName); } public void GenerateStart() diff --git a/src/Generator/Generators/CLI/CLITypePrinter.cs b/src/Generator/Generators/CLI/CLITypePrinter.cs index eb00ec89..7af46564 100644 --- a/src/Generator/Generators/CLI/CLITypePrinter.cs +++ b/src/Generator/Generators/CLI/CLITypePrinter.cs @@ -193,7 +193,7 @@ namespace Cxxi.Generators.CLI public string VisitDeclaration(Declaration decl) { var name = decl.Visit(this); - return string.Format("{0}::{1}", Library.Name, name); + return string.Format("{0}::{1}", decl.Namespace.QualifiedName, name); } public string VisitClassDecl(Class @class)