Browse Source

Fixed the generation of code in a single file.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/288/head
Dimitar Dobrev 11 years ago
parent
commit
ef4be0b959
  1. 52
      src/Generator/Generator.cs
  2. 6
      src/Generator/Generators/CLI/CLIGenerator.cs
  3. 6
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs
  4. 6
      src/Generator/Generators/CLI/CLISourcesTemplate.cs
  5. 6
      src/Generator/Generators/CLI/CLITextTemplate.cs
  6. 4
      src/Generator/Generators/CSharp/CSharpGenerator.cs
  7. 24
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  8. 19
      src/Generator/Generators/Template.cs
  9. 5
      tests/CSharpTemp/AnotherUnit.cpp
  10. 3
      tests/CSharpTemp/AnotherUnit.h
  11. 6
      tests/CSharpTemp/CSharpTemp.Tests.cs
  12. 1
      tests/CSharpTemp/CSharpTemp.cs

52
src/Generator/Generator.cs

@ -67,30 +67,41 @@ namespace CppSharp.Generators @@ -67,30 +67,41 @@ namespace CppSharp.Generators
{
var outputs = new List<GeneratorOutput>();
foreach (var unit in Driver.ASTContext.TranslationUnits)
var units = Driver.ASTContext.TranslationUnits.Where(
u => u.IsGenerated && u.HasDeclarations && !u.IsSystemHeader && u.IsValid);
if (Driver.Options.IsCSharpGenerator && Driver.Options.GenerateSingleCSharpFile)
GenerateSingleTemplate(units, outputs);
else
foreach (var unit in units)
GenerateTemplate(unit, outputs);
return outputs;
}
private void GenerateSingleTemplate(IEnumerable<TranslationUnit> units, ICollection<GeneratorOutput> outputs)
{
var output = new GeneratorOutput
{
TranslationUnit = new TranslationUnit
{
if (!unit.IsGenerated || !unit.HasDeclarations)
continue;
FilePath = string.Format("{0}.cs", Driver.Options.OutputNamespace ?? Driver.Options.LibraryName)
},
Templates = Generate(units)
};
output.Templates[0].Process();
outputs.Add(output);
if (unit.IsSystemHeader || !unit.IsValid)
continue;
OnUnitGenerated(output);
}
var templates = Generate(unit);
private void GenerateTemplate(TranslationUnit unit, ICollection<GeneratorOutput> outputs)
{
var templates = Generate(new[] { unit });
if (templates.Count == 0)
continue;
return;
if (templates.Count == 1)
{
templates[0].Process(Template.Order.First | Template.Order.Last);
}
else
foreach (var template in templates)
{
templates.First().Process(Template.Order.First);
for (var i = 1; i < templates.Count - 1; i++)
{
templates[i].Process(Template.Order.InBetween);
}
templates.Last().Process(Template.Order.Last);
template.Process();
}
var output = new GeneratorOutput
@ -103,13 +114,10 @@ namespace CppSharp.Generators @@ -103,13 +114,10 @@ namespace CppSharp.Generators
OnUnitGenerated(output);
}
return outputs;
}
/// <summary>
/// Generates the outputs for a given translation unit.
/// </summary>
public abstract List<Template> Generate(TranslationUnit unit);
public abstract List<Template> Generate(IEnumerable<TranslationUnit> units);
public static string GeneratedIdentifier(string id)
{

6
src/Generator/Generators/CLI/CLIGenerator.cs

@ -17,14 +17,14 @@ namespace CppSharp.Generators.CLI @@ -17,14 +17,14 @@ namespace CppSharp.Generators.CLI
Type.TypePrinterDelegate += type => type.Visit(typePrinter);
}
public override List<Template> Generate(TranslationUnit unit)
public override List<Template> Generate(IEnumerable<TranslationUnit> units)
{
var outputs = new List<Template>();
var header = new CLIHeadersTemplate(Driver, unit);
var header = new CLIHeadersTemplate(Driver, units);
outputs.Add(header);
var source = new CLISourcesTemplate(Driver, unit);
var source = new CLISourcesTemplate(Driver, units);
outputs.Add(source);
return outputs;

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

@ -15,12 +15,12 @@ namespace CppSharp.Generators.CLI @@ -15,12 +15,12 @@ namespace CppSharp.Generators.CLI
{
public override string FileExtension { get { return "h"; } }
public CLIHeadersTemplate(Driver driver, TranslationUnit unit)
: base(driver, unit)
public CLIHeadersTemplate(Driver driver, IEnumerable<TranslationUnit> units)
: base(driver, units)
{
}
public override void Process(Order order)
public override void Process()
{
PushBlock(BlockKind.Header);
PopBlock();

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

@ -16,13 +16,13 @@ namespace CppSharp.Generators.CLI @@ -16,13 +16,13 @@ namespace CppSharp.Generators.CLI
/// </summary>
public class CLISourcesTemplate : CLITextTemplate
{
public CLISourcesTemplate(Driver driver, TranslationUnit unit)
: base(driver, unit)
public CLISourcesTemplate(Driver driver, IEnumerable<TranslationUnit> units)
: base(driver, units)
{
}
public override void Process(Order order)
public override void Process()
{
PushBlock(BlockKind.Header);
PopBlock();

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

@ -61,8 +61,8 @@ namespace CppSharp.Generators.CLI @@ -61,8 +61,8 @@ namespace CppSharp.Generators.CLI
public ISet<Include> Includes;
protected CLITextTemplate(Driver driver, TranslationUnit unit)
: base(driver, unit)
protected CLITextTemplate(Driver driver, IEnumerable<TranslationUnit> units)
: base(driver, units)
{
TypePrinter = new CLITypePrinter(driver);
Includes = new HashSet<Include>();
@ -70,7 +70,7 @@ namespace CppSharp.Generators.CLI @@ -70,7 +70,7 @@ namespace CppSharp.Generators.CLI
public abstract override string FileExtension { get; }
public abstract override void Process(Order order);
public abstract override void Process();
#region Helpers

4
src/Generator/Generators/CSharp/CSharpGenerator.cs

@ -16,11 +16,11 @@ namespace CppSharp.Generators.CSharp @@ -16,11 +16,11 @@ namespace CppSharp.Generators.CSharp
CppSharp.AST.Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type;
}
public override List<Template> Generate(TranslationUnit unit)
public override List<Template> Generate(IEnumerable<TranslationUnit> units)
{
var outputs = new List<Template>();
var template = new CSharpTextTemplate(Driver, unit, typePrinter, expressionPrinter);
var template = new CSharpTextTemplate(Driver, units, typePrinter, expressionPrinter);
outputs.Add(template);
return outputs;

24
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -91,8 +91,8 @@ namespace CppSharp.Generators.CSharp @@ -91,8 +91,8 @@ namespace CppSharp.Generators.CSharp
get { return "cs"; }
}
public CSharpTextTemplate(Driver driver, TranslationUnit unit, CSharpTypePrinter typePrinter, CSharpExpressionPrinter expressionPrinter)
: base(driver, unit)
public CSharpTextTemplate(Driver driver, IEnumerable<TranslationUnit> units, CSharpTypePrinter typePrinter, CSharpExpressionPrinter expressionPrinter)
: base(driver, units)
{
TypePrinter = typePrinter;
ExpressionPrinter = expressionPrinter;
@ -126,12 +126,10 @@ namespace CppSharp.Generators.CSharp @@ -126,12 +126,10 @@ namespace CppSharp.Generators.CSharp
#endregion
public override void Process(Order order)
public override void Process()
{
GenerateHeader();
if (!Options.GenerateSingleCSharpFile || order.HasFlag(Order.First))
{
PushBlock(CSharpBlockKind.Usings);
WriteLine("using System;");
WriteLine("using System.Runtime.InteropServices;");
@ -148,19 +146,18 @@ namespace CppSharp.Generators.CSharp @@ -148,19 +146,18 @@ namespace CppSharp.Generators.CSharp
WriteLine("namespace {0}", Driver.Options.OutputNamespace);
WriteStartBraceIndent();
}
}
GenerateDeclContext(TranslationUnit);
if (!Options.GenerateSingleCSharpFile || order.HasFlag(Order.Last))
foreach (var unit in TranslationUnits)
{
GenerateDeclContext(unit);
}
if (Options.GenerateLibraryNamespace)
{
WriteCloseBraceIndent();
PopBlock(NewLineKind.BeforeNextBlock);
}
}
}
public void GenerateHeader()
{
@ -217,7 +214,7 @@ namespace CppSharp.Generators.CSharp @@ -217,7 +214,7 @@ namespace CppSharp.Generators.CSharp
{
PushBlock(CSharpBlockKind.Functions);
WriteLine("public unsafe partial class {0}",
TranslationUnit.FileNameWithoutExtension);
TranslationUnits[0].FileNameWithoutExtension);
WriteStartBraceIndent();
PushBlock(CSharpBlockKind.InternalsClass);
@ -459,6 +456,8 @@ namespace CppSharp.Generators.CSharp @@ -459,6 +456,8 @@ namespace CppSharp.Generators.CSharp
typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
GenerateClassFields(@class, GenerateClassInternalsField, true);
if (@class.IsGenerated)
{
if (Options.GenerateVirtualTables && @class.IsDynamic)
GenerateVTablePointers(@class);
@ -468,6 +467,7 @@ namespace CppSharp.Generators.CSharp @@ -468,6 +467,7 @@ namespace CppSharp.Generators.CSharp
{
GenerateInternalFunction(function);
}
}
typePrinter.PopContext();
@ -583,7 +583,7 @@ namespace CppSharp.Generators.CSharp @@ -583,7 +583,7 @@ namespace CppSharp.Generators.CSharp
if (@class != null && @class.HasBaseClass)
Write("new ");
WriteLine("struct Internal");
WriteLine("partial struct Internal");
}
private void GenerateStructMarshalingProperties(Class @class)

19
src/Generator/Generators/Template.cs

@ -247,17 +247,12 @@ namespace CppSharp.Generators @@ -247,17 +247,12 @@ namespace CppSharp.Generators
public abstract class Template : ITextGenerator
{
[Flags]
public enum Order
{
First = 1,
InBetween = 2,
Last = 4
}
public Driver Driver { get; private set; }
public DriverOptions Options { get; private set; }
public TranslationUnit TranslationUnit { get; private set; }
public List<TranslationUnit> TranslationUnits { get; private set; }
public TranslationUnit TranslationUnit { get { return TranslationUnits[0]; } }
public IDiagnosticConsumer Log
{
get { return Driver.Diagnostics; }
@ -268,16 +263,16 @@ namespace CppSharp.Generators @@ -268,16 +263,16 @@ namespace CppSharp.Generators
public abstract string FileExtension { get; }
protected Template(Driver driver, TranslationUnit unit)
protected Template(Driver driver, IEnumerable<TranslationUnit> units)
{
Driver = driver;
Options = driver.Options;
TranslationUnit = unit;
TranslationUnits = new List<TranslationUnit>(units);
RootBlock = new Block();
ActiveBlock = RootBlock;
}
public abstract void Process(Order order);
public abstract void Process();
public string Generate()
{

5
tests/CSharpTemp/AnotherUnit.cpp

@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
#include "AnotherUnit.h"
void functionInAnotherUnit()
{
}

3
tests/CSharpTemp/AnotherUnit.h

@ -0,0 +1,3 @@ @@ -0,0 +1,3 @@
#include "../Tests.h"
void DLL_API functionInAnotherUnit();

6
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -138,4 +138,10 @@ public class CSharpTempTests : GeneratorTestFixture @@ -138,4 +138,10 @@ public class CSharpTempTests : GeneratorTestFixture
methodsWithDefaultValues.DefaultValueType();
methodsWithDefaultValues.DefaultIntAssignedAnEnum();
}
[Test]
public void TestGenerationOfAnotherUnitInSameFile()
{
AnotherUnit.FunctionInAnotherUnit();
}
}

1
tests/CSharpTemp/CSharpTemp.cs

@ -69,6 +69,7 @@ namespace CppSharp.Tests @@ -69,6 +69,7 @@ namespace CppSharp.Tests
driver.TranslationUnitPasses.AddPass(new TestAttributesPass());
driver.Options.MarshalCharAsManagedChar = true;
driver.Options.GenerateDefaultValuesForArguments = true;
driver.Options.GenerateSingleCSharpFile = true;
}
public override void Preprocess(Driver driver, ASTContext ctx)

Loading…
Cancel
Save