Browse Source

Simplified the printing of template internals.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
cpp_module_crash
Dimitar Dobrev 10 years ago
parent
commit
bd3c921731
  1. 21
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  2. 76
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs

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

@ -72,27 +72,21 @@ namespace CppSharp.Generators.CSharp @@ -72,27 +72,21 @@ namespace CppSharp.Generators.CSharp
return types.Replace("global::System.", string.Empty).Replace("*", "Ptr").Replace('.', '_');
}
public static string GetSuffixForInternal(ClassTemplateSpecialization templateSpecialization,
public static string GetSuffixForInternal(ClassTemplateSpecialization specialization,
CSharpTypePrinter typePrinter)
{
return templateSpecialization == null ? string.Empty :
GetSuffixForInternal(templateSpecialization.TemplatedDecl.TemplatedDecl,
templateSpecialization.Arguments, typePrinter);
}
public static string GetSuffixForInternal(Declaration template,
IEnumerable<TemplateArgument> args, CSharpTypePrinter typePrinter)
{
if (((Class) template).Fields.All(f => !f.IsDependent || f.Type.IsAddress()))
if (specialization.TemplatedDecl.TemplatedClass.Fields.All(
f => !f.IsDependent || f.Type.IsAddress()))
return string.Empty;
if (args.All(a => a.Type.Type != null && a.Type.Type.IsAddress()))
if (specialization.Arguments.All(
a => a.Type.Type != null && a.Type.Type.IsAddress()))
return "_Ptr";
// we don't want internals in the names of internals :)
typePrinter.PushContext(CSharpTypePrinterContextKind.Managed);
var suffix = new StringBuilder();
foreach (var argType in from argType in args
foreach (var argType in from argType in specialization.Arguments
where argType.Type.Type != null
select argType.Type.ToString())
{
@ -674,7 +668,8 @@ namespace CppSharp.Generators.CSharp @@ -674,7 +668,8 @@ namespace CppSharp.Generators.CSharp
Write("new ");
var templateSpecialization = @class as ClassTemplateSpecialization;
var suffix = Helpers.GetSuffixForInternal(templateSpecialization, TypePrinter);
var suffix = templateSpecialization == null ? string.Empty :
Helpers.GetSuffixForInternal(templateSpecialization, TypePrinter);
WriteLine("{0}partial struct Internal{1}",
templateSpecialization != null ? "unsafe " : string.Empty, suffix);
}

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

@ -387,12 +387,10 @@ namespace CppSharp.Generators.CSharp @@ -387,12 +387,10 @@ namespace CppSharp.Generators.CSharp
{
if (ContextKind != CSharpTypePrinterContextKind.Native)
return GetNestedQualifiedName(decl);
// HACK: we can actually get the specialization directly by using TemplateSpecializationType.GetClassTemplateSpecialization()
// however, that returns the original specialisation which Clang places in the original name-space
// so if we specialise a template located in a dependency, we get uncompilable code
// so let's get the specialisation object from the current name-space by matching by name and template arguments
// this will be fixed when we have support for generating multiple libraries from a single AST
return GetTemplateSpecializationInternal(template);
var specialization = template.GetClassTemplateSpecialization();
return string.Format("{0}.Internal{1}",
GetNestedQualifiedName(specialization.TemplatedDecl.Namespace, specialization),
Helpers.GetSuffixForInternal(specialization, this));
}
typeMap.Declaration = decl;
@ -416,65 +414,6 @@ namespace CppSharp.Generators.CSharp @@ -416,65 +414,6 @@ namespace CppSharp.Generators.CSharp
".Internal" : string.Empty);
}
private string GetTemplateSpecializationInternal(TemplateSpecializationType template)
{
var classTemplate = template.Template as ClassTemplate;
if (classTemplate != null)
{
foreach (var specialization in classTemplate.Specializations)
{
if (FoundMatchingSpecialization(template.Arguments,
specialization.Arguments, classTemplate.Parameters))
{
return GetNestedQualifiedName(specialization.TemplatedDecl) +
".Internal" + Helpers.GetSuffixForInternal(
template.Template.TemplatedDecl, specialization.Arguments, this);
}
}
}
var functionTemplate = (FunctionTemplate) template.Template;
foreach (var specialization in functionTemplate.Specializations)
{
if (FoundMatchingSpecialization(template.Arguments,
specialization.Arguments, functionTemplate.Parameters))
{
return GetNestedQualifiedName(specialization.SpecializedFunction) +
".Internal" + Helpers.GetSuffixForInternal(
template.Template.TemplatedDecl, specialization.Arguments, this);
}
}
var qualifiedName = GetNestedQualifiedName(template.Template.TemplatedDecl);
return qualifiedName + ".Internal" +
Helpers.GetSuffixForInternal(template.Template.TemplatedDecl, template.Arguments, this);
}
private static bool FoundMatchingSpecialization(
IList<TemplateArgument> templateTypeArguments,
IEnumerable<TemplateArgument> templateSpecializationArguments,
IList<TemplateParameter> templateParameters)
{
var usedTemplateArguments = new List<TemplateArgument>(templateSpecializationArguments);
for (int i = usedTemplateArguments.Count - 1; i >= templateTypeArguments.Count; i--)
{
var templateParameter = templateParameters[i];
var typeTemplateParameter = templateParameter as TypeTemplateParameter;
if (typeTemplateParameter != null &&
typeTemplateParameter.DefaultArgument.Type != null)
{
usedTemplateArguments.RemoveAt(i);
continue;
}
var nonTypeTemplateParameter = templateParameter as NonTypeTemplateParameter;
if (nonTypeTemplateParameter != null &&
nonTypeTemplateParameter.DefaultArgument != null)
{
usedTemplateArguments.RemoveAt(i);
continue;
}
}
return usedTemplateArguments.SequenceEqual(templateTypeArguments);
}
private string GetCSharpSignature(TypeMap typeMap)
{
Context.CSharpKind = ContextKind;
@ -674,10 +613,15 @@ namespace CppSharp.Generators.CSharp @@ -674,10 +613,15 @@ namespace CppSharp.Generators.CSharp
}
private string GetNestedQualifiedName(Declaration decl)
{
return GetNestedQualifiedName(decl.Namespace, decl);
}
private string GetNestedQualifiedName(Declaration @namespace, Declaration decl)
{
var names = new List<string> { decl.Name };
var ctx = decl.Namespace;
var ctx = @namespace;
while (ctx != null)
{
if (!string.IsNullOrWhiteSpace(ctx.Name))

Loading…
Cancel
Save