From 1ebf418e180231a8f96aed30ff480cd864e6fe4c Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Sat, 16 Dec 2017 23:30:47 +0200 Subject: [PATCH] Considered dependent fields when generating internals for types nested in templates. Signed-off-by: Dimitar Dobrev --- .../Generators/CSharp/CSharpSources.cs | 23 +++++++++++-------- src/Generator/Generators/CodeGenerator.cs | 20 ++++++++-------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index f2376545..601a0e08 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -286,7 +286,7 @@ namespace CppSharp.Generators.CSharp IList specializations) { PushBlock(BlockKind.Namespace); - var generated = GetGenerated(specializations); + var generated = GetGeneratedClasses(classTemplate, specializations); WriteLine("namespace {0}{1}", classTemplate.OriginalNamespace is Class && !classTemplate.OriginalNamespace.IsDependent ? @@ -304,7 +304,11 @@ namespace CppSharp.Generators.CSharp foreach (var group in generated.SelectMany(s => s.Classes).Where( c => !c.IsIncomplete).GroupBy(c => c.Name)) - GenerateNestedInternals(group.Key, group); + { + var nested = classTemplate.Classes.FirstOrDefault(c => c.Name == group.Key); + if (nested != null) + GenerateNestedInternals(group.Key, GetGeneratedClasses(nested, group)); + } WriteCloseBraceIndent(); PopBlock(NewLineKind.BeforeNextBlock); @@ -324,17 +328,16 @@ namespace CppSharp.Generators.CSharp NewLine(); } - private IEnumerable GetGenerated( - IList specializations) + private IEnumerable GetGeneratedClasses( + Class dependentClass, IEnumerable specializedClasses) { - var specialization = specializations.FirstOrDefault(s => s.IsGenerated) ?? - specializations[0]; + var specialization = specializedClasses.FirstOrDefault(s => s.IsGenerated) ?? + specializedClasses.First(); - Class classTemplate = specialization.TemplatedDecl.TemplatedClass; - if (classTemplate.HasDependentValueFieldInLayout()) - return specializations; + if (dependentClass.HasDependentValueFieldInLayout()) + return specializedClasses; - return new List { specialization }; + return new[] { specialization }; } public override void GenerateDeclarationCommon(Declaration decl) diff --git a/src/Generator/Generators/CodeGenerator.cs b/src/Generator/Generators/CodeGenerator.cs index 61d5cd17..a5952c32 100644 --- a/src/Generator/Generators/CodeGenerator.cs +++ b/src/Generator/Generators/CodeGenerator.cs @@ -530,18 +530,20 @@ namespace CppSharp.Generators public static string GetSuffixForInternal(DeclarationContext @class) { - ClassTemplateSpecialization specialization = null; - DeclarationContext declContext = @class; - while (declContext != null) + if (@class == null) + return string.Empty; + + Class template = null; + var specialization = @class as ClassTemplateSpecialization ?? + @class.Namespace as ClassTemplateSpecialization; + if (specialization != null) { - specialization = declContext as ClassTemplateSpecialization; - if (specialization != null) - break; - declContext = declContext.Namespace; + template = specialization.TemplatedDecl.TemplatedClass; + if (@class != specialization) + template = template.Classes.FirstOrDefault(c => c.Name == @class.Name); } - if (specialization == null || - !specialization.TemplatedDecl.TemplatedClass.HasDependentValueFieldInLayout()) + if (template == null || !template.HasDependentValueFieldInLayout()) return string.Empty; if (specialization.Arguments.All(