From bd3c921731721f9788da0034ff2fe21cd6138c3c Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Fri, 13 May 2016 01:15:45 +0300 Subject: [PATCH] Simplified the printing of template internals. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpTextTemplate.cs | 21 ++--- .../Generators/CSharp/CSharpTypePrinter.cs | 76 +++---------------- 2 files changed, 18 insertions(+), 79 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index 0aba7900..e2bcb15d 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -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 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 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); } diff --git a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs index a081b0c9..b9344b67 100644 --- a/src/Generator/Generators/CSharp/CSharpTypePrinter.cs +++ b/src/Generator/Generators/CSharp/CSharpTypePrinter.cs @@ -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 ".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 templateTypeArguments, - IEnumerable templateSpecializationArguments, - IList templateParameters) - { - var usedTemplateArguments = new List(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 } private string GetNestedQualifiedName(Declaration decl) + { + return GetNestedQualifiedName(decl.Namespace, decl); + } + + private string GetNestedQualifiedName(Declaration @namespace, Declaration decl) { var names = new List { decl.Name }; - var ctx = decl.Namespace; + var ctx = @namespace; while (ctx != null) { if (!string.IsNullOrWhiteSpace(ctx.Name))