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

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

@ -387,12 +387,10 @@ namespace CppSharp.Generators.CSharp
{ {
if (ContextKind != CSharpTypePrinterContextKind.Native) if (ContextKind != CSharpTypePrinterContextKind.Native)
return GetNestedQualifiedName(decl); return GetNestedQualifiedName(decl);
// HACK: we can actually get the specialization directly by using TemplateSpecializationType.GetClassTemplateSpecialization() var specialization = template.GetClassTemplateSpecialization();
// however, that returns the original specialisation which Clang places in the original name-space return string.Format("{0}.Internal{1}",
// so if we specialise a template located in a dependency, we get uncompilable code GetNestedQualifiedName(specialization.TemplatedDecl.Namespace, specialization),
// so let's get the specialisation object from the current name-space by matching by name and template arguments Helpers.GetSuffixForInternal(specialization, this));
// this will be fixed when we have support for generating multiple libraries from a single AST
return GetTemplateSpecializationInternal(template);
} }
typeMap.Declaration = decl; typeMap.Declaration = decl;
@ -416,65 +414,6 @@ namespace CppSharp.Generators.CSharp
".Internal" : string.Empty); ".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) private string GetCSharpSignature(TypeMap typeMap)
{ {
Context.CSharpKind = ContextKind; Context.CSharpKind = ContextKind;
@ -674,10 +613,15 @@ namespace CppSharp.Generators.CSharp
} }
private string GetNestedQualifiedName(Declaration decl) 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 names = new List<string> { decl.Name };
var ctx = decl.Namespace; var ctx = @namespace;
while (ctx != null) while (ctx != null)
{ {
if (!string.IsNullOrWhiteSpace(ctx.Name)) if (!string.IsNullOrWhiteSpace(ctx.Name))

Loading…
Cancel
Save