Browse Source

Handled internals of nested template specialisations.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/658/head
Dimitar Dobrev 9 years ago
parent
commit
e50e9416d9
  1. 1
      src/Generator/Driver.cs
  2. 5
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  3. 37
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  4. 66
      src/Generator/Passes/MoveNestedTemplateInternalsPass.cs
  5. 5
      tests/CSharp/CSharpTemplates.h

1
src/Generator/Driver.cs

@ -282,6 +282,7 @@ namespace CppSharp @@ -282,6 +282,7 @@ namespace CppSharp
if (Options.GenerateInlines)
TranslationUnitPasses.AddPass(new GenerateInlinesCodePass());
TranslationUnitPasses.AddPass(new TrimSpecializationsPass());
TranslationUnitPasses.AddPass(new MoveNestedTemplateInternalsPass());
TranslationUnitPasses.AddPass(new GenerateTemplatesCodePass());
}

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

@ -348,7 +348,10 @@ namespace CppSharp.Generators.CSharp @@ -348,7 +348,10 @@ namespace CppSharp.Generators.CSharp
return;
PushBlock(CSharpBlockKind.Namespace);
WriteLine("namespace {0}", classTemplate.Name);
WriteLine("namespace {0}{1}",
classTemplate.OriginalNamespace is Class ?
classTemplate.OriginalNamespace.Name + '_' : string.Empty,
classTemplate.Name);
WriteStartBraceIndent();
foreach (var specialization in specializations)

37
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -388,7 +388,7 @@ namespace CppSharp.Generators.CSharp @@ -388,7 +388,7 @@ namespace CppSharp.Generators.CSharp
return GetNestedQualifiedName(decl);
var specialization = template.GetClassTemplateSpecialization();
return string.Format("{0}.Internal{1}",
GetNestedQualifiedName(specialization.TemplatedDecl.Namespace, specialization),
GetNestedQualifiedName(specialization),
Helpers.GetSuffixForInternal(specialization, this));
}
@ -611,21 +611,30 @@ namespace CppSharp.Generators.CSharp @@ -611,21 +611,30 @@ namespace CppSharp.Generators.CSharp
return GetNestedQualifiedName(@enum);
}
public string GetNestedQualifiedName(ClassTemplateSpecialization decl)
public string GetNestedQualifiedName(Declaration decl)
{
return GetNestedQualifiedName(decl.Namespace, decl);
}
private string GetNestedQualifiedName(Declaration decl)
{
return GetNestedQualifiedName(decl.Namespace, decl);
}
var names = new List<string>();
private string GetNestedQualifiedName(Declaration @namespace, Declaration decl)
{
var names = new List<string> { decl.Name };
var ctx = @namespace;
Declaration ctx;
var specialization = decl as ClassTemplateSpecialization;
if (specialization != null)
{
ctx = specialization.TemplatedDecl.Namespace;
if (specialization.OriginalNamespace is Class)
{
names.Add(string.Format("{0}_{1}", decl.OriginalNamespace.Name, decl.Name));
ctx = ctx.Namespace;
}
else
{
names.Add(decl.Name);
}
}
else
{
names.Add(decl.Name);
ctx = decl.Namespace;
}
while (ctx != null)
{
if (!string.IsNullOrWhiteSpace(ctx.Name))

66
src/Generator/Passes/MoveNestedTemplateInternalsPass.cs

@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
using System.Collections.Generic;
using System.Linq;
using CppSharp.AST;
namespace CppSharp.Passes
{
public class MoveNestedTemplateInternalsPass : TranslationUnitPass
{
public MoveNestedTemplateInternalsPass()
{
Options.VisitClassBases = false;
Options.VisitClassFields = false;
Options.VisitClassMethods = false;
Options.VisitClassProperties = false;
Options.VisitFunctionParameters = false;
Options.VisitFunctionReturnType = false;
Options.VisitNamespaceEnums = false;
Options.VisitNamespaceEvents = false;
Options.VisitNamespaceTypedefs = false;
Options.VisitNamespaceVariables = false;
Options.VisitTemplateArguments = false;
}
public override bool VisitLibrary(ASTContext context)
{
var result = base.VisitLibrary(context);
foreach (var entry in movedClassTemplates)
{
foreach (var template in entry.Value)
{
foreach (var decl in new[] { template, template.TemplatedDecl })
{
int index = entry.Key.Declarations.IndexOf(decl.Namespace);
decl.Namespace.Declarations.Remove(decl);
decl.Namespace = entry.Key;
entry.Key.Declarations.Insert(index, decl);
}
}
}
return result;
}
public override bool VisitClassTemplateDecl(ClassTemplate template)
{
if (!base.VisitClassTemplateDecl(template) ||
template.Specializations.Count == 0 ||
template.Specializations.All(s => s is ClassTemplatePartialSpecialization))
return false;
var @class = template.TemplatedDecl.Namespace as Class;
if (@class == null || @class is ClassTemplateSpecialization ||
@class.Namespace is Class)
return false;
if (movedClassTemplates.ContainsKey(@class.Namespace))
movedClassTemplates[@class.Namespace].Add(template);
else
movedClassTemplates.Add(@class.Namespace, new List<ClassTemplate> { template });
return true;
}
private Dictionary<DeclarationContext, IList<ClassTemplate>> movedClassTemplates =
new Dictionary<DeclarationContext, IList<ClassTemplate>>();
}
}

5
tests/CSharp/CSharpTemplates.h

@ -50,6 +50,10 @@ class DLL_API TemplateSpecializer @@ -50,6 +50,10 @@ class DLL_API TemplateSpecializer
{
public:
TemplateSpecializer();
template <typename T>
class NestedTemplate
{
};
private:
IndependentFields<int> independentFields;
DependentValueFields<bool> dependentValueFields;
@ -59,6 +63,7 @@ private: @@ -59,6 +63,7 @@ private:
DependentValueFields<T2> dependentPointerFieldsT2;
DependentValueFields<IndependentFields<int>> specializeWithSpecialization;
DependentValueFields<IndependentFields<bool>> specializeWithSameSpecialization;
NestedTemplate<int> nestedTemplate;
void completeSpecializationInParameter(DependentValueFields<float> p1,
DependentValueFields<int*> p2,
DependentValueFields<float*> p3);

Loading…
Cancel
Save