Browse Source

Rework the CLI generator to actually handle (nested) namespaces propertly.

pull/1/head
triton 13 years ago
parent
commit
b208d7b588
  1. 67
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  2. 10
      src/Generator/Generators/CLI/CLIMarshal.cs
  3. 32
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  4. 4
      src/Generator/Generators/CLI/CLITextTemplate.cs
  5. 2
      src/Generator/Generators/CLI/CLITypePrinter.cs

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

@ -28,10 +28,7 @@ namespace Cxxi.Generators.CLI @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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);
}

10
src/Generator/Generators/CLI/CLIMarshal.cs

@ -182,12 +182,20 @@ namespace Cxxi.Generators.CLI @@ -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);
}

32
src/Generator/Generators/CLI/CLISourcesTemplate.cs

@ -74,11 +74,13 @@ namespace Cxxi.Generators.CLI @@ -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 @@ -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 @@ -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 @@ -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)

4
src/Generator/Generators/CLI/CLITextTemplate.cs

@ -45,7 +45,9 @@ public abstract class CLITextTemplate : TextTemplate @@ -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()

2
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -193,7 +193,7 @@ namespace Cxxi.Generators.CLI @@ -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)

Loading…
Cancel
Save