Browse Source

Collected internal functions of class template specialisations.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1137/head
Dimitar Dobrev 9 years ago
parent
commit
e0c397d709
  1. 106
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 5
      src/Generator/Generators/CodeGenerator.cs

106
src/Generator/Generators/CSharp/CSharpSources.cs

@ -207,19 +207,7 @@ namespace CppSharp.Generators.CSharp @@ -207,19 +207,7 @@ namespace CppSharp.Generators.CSharp
if (classTemplate.Specializations.Count == 0)
return;
List<ClassTemplateSpecialization> specializations;
if (classTemplate.Fields.Any(
f => f.Type.Desugar() is TemplateParameterType))
specializations = classTemplate.Specializations;
else
{
specializations = new List<ClassTemplateSpecialization>();
var specialization = classTemplate.Specializations.FirstOrDefault(s => !s.Ignore);
if (specialization == null)
specializations.Add(classTemplate.Specializations[0]);
else
specializations.Add(specialization);
}
var specializations = GetSpecializationsToGenerate(classTemplate);
bool generateClass = specializations.Any(s => s.IsGenerated);
if (!generateClass)
@ -257,6 +245,25 @@ namespace CppSharp.Generators.CSharp @@ -257,6 +245,25 @@ namespace CppSharp.Generators.CSharp
}
}
private static List<ClassTemplateSpecialization> GetSpecializationsToGenerate(Class classTemplate)
{
List<ClassTemplateSpecialization> specializations;
if (classTemplate.Fields.Any(
f => f.Type.Desugar() is TemplateParameterType))
specializations = classTemplate.Specializations;
else
{
specializations = new List<ClassTemplateSpecialization>();
var specialization = classTemplate.Specializations.FirstOrDefault(s => !s.Ignore);
if (specialization == null)
specializations.Add(classTemplate.Specializations[0]);
else
specializations.Add(specialization);
}
return specializations;
}
public override void GenerateDeclarationCommon(Declaration decl)
{
base.GenerateDeclarationCommon(decl);
@ -458,6 +465,22 @@ namespace CppSharp.Generators.CSharp @@ -458,6 +465,22 @@ namespace CppSharp.Generators.CSharp
foreach (var @base in @class.Bases.Where(b => b.IsClass && !b.Class.Ignore))
functions.AddRange(GatherClassInternalFunctions(@base.Class, false));
var currentSpecialization = @class as ClassTemplateSpecialization;
Class template;
if (currentSpecialization != null &&
GetSpecializationsToGenerate(
template = currentSpecialization.TemplatedDecl.TemplatedClass).Count == 1)
foreach (var specialization in template.Specializations.Where(s => !s.Ignore))
GatherClassInternalFunctions(specialization, includeCtors, functions);
else
GatherClassInternalFunctions(@class, includeCtors, functions);
return functions;
}
private void GatherClassInternalFunctions(Class @class, bool includeCtors,
List<Function> functions)
{
Action<Method> tryAddOverload = method =>
{
if (method.IsSynthetized &&
@ -514,8 +537,6 @@ namespace CppSharp.Generators.CSharp @@ -514,8 +537,6 @@ namespace CppSharp.Generators.CSharp
if (prop.SetMethod != null && prop.SetMethod != prop.GetMethod)
tryAddOverload(prop.SetMethod);
}
return functions;
}
private IEnumerable<string> GatherInternalParams(Function function, out TypePrinterResult retType)
@ -1226,7 +1247,7 @@ namespace CppSharp.Generators.CSharp @@ -1226,7 +1247,7 @@ namespace CppSharp.Generators.CSharp
return p;
});
return string.Format("this[{0}]", FormatMethodParameters(@params));
return $"this[{FormatMethodParameters(@params)}]";
}
private void GenerateVariable(Class @class, Variable variable)
@ -1619,7 +1640,7 @@ namespace CppSharp.Generators.CSharp @@ -1619,7 +1640,7 @@ namespace CppSharp.Generators.CSharp
public string GetVTableMethodDelegateName(Function function)
{
var nativeId = GetFunctionNativeIdentifier(function);
var nativeId = GetFunctionNativeIdentifier(function, true);
// Trim '@' (if any) because '@' is valid only as the first symbol.
nativeId = nativeId.Trim('@');
@ -2953,38 +2974,53 @@ namespace CppSharp.Generators.CSharp @@ -2953,38 +2974,53 @@ namespace CppSharp.Generators.CSharp
return function.Name;
}
public static string GetFunctionNativeIdentifier(Function function)
public static string GetFunctionNativeIdentifier(Function function,
bool ignoreSpecialization = false)
{
var functionName = function.Name;
var identifier = new StringBuilder();
var method = function as Method;
if (method != null)
if (function.IsOperator)
identifier.Append($"Operator{function.OperatorKind}");
else
{
if (method.IsConstructor && !method.IsCopyConstructor)
functionName = "ctor";
else if (method.IsCopyConstructor)
functionName = "cctor";
else if (method.IsDestructor)
functionName = "dtor";
var method = function as Method;
if (method != null)
{
if (method.IsConstructor && !method.IsCopyConstructor)
identifier.Append("ctor");
else if (method.IsCopyConstructor)
identifier.Append("cctor");
else if (method.IsDestructor)
identifier.Append("dtor");
else
identifier.Append(GetMethodIdentifier(method));
}
else
functionName = GetMethodIdentifier(method);
{
identifier.Append(function.Name);
}
}
var identifier = functionName;
if (function.IsOperator)
identifier = "Operator" + function.OperatorKind;
var specialization = function.Namespace as ClassTemplateSpecialization;
if (specialization != null && !ignoreSpecialization)
identifier.Append(Helpers.GetSuffixFor(specialization));
var overloads = function.Namespace.GetOverloads(function)
.ToList();
var index = overloads.IndexOf(function);
if (index >= 0)
identifier += "_" + index.ToString(CultureInfo.InvariantCulture);
{
identifier.Append('_');
identifier.Append(index.ToString(CultureInfo.InvariantCulture));
}
else if (function.Index.HasValue)
identifier += "_" + function.Index.Value;
{
identifier.Append('_');
identifier.Append(function.Index.Value);
}
return identifier;
return identifier.ToString();
}
public void GenerateInternalFunction(Function function)

5
src/Generator/Generators/CodeGenerator.cs

@ -407,6 +407,11 @@ namespace CppSharp.Generators @@ -407,6 +407,11 @@ namespace CppSharp.Generators
a => a.Type.Type != null && a.Type.Type.IsAddress()))
return "_Ptr";
return GetSuffixFor(specialization);
}
public static string GetSuffixFor(ClassTemplateSpecialization specialization)
{
var suffixBuilder = new StringBuilder(specialization.USR);
for (int i = 0; i < suffixBuilder.Length; i++)
if (!char.IsLetterOrDigit(suffixBuilder[i]))

Loading…
Cancel
Save