From 09190268bc2599930afc1954aa93917a34d3e67d Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Fri, 29 Oct 2021 17:45:11 +0300 Subject: [PATCH] Generate valid C# for internal fields of type external specialization Signed-off-by: Dimitar Dobrev --- src/Generator/Extensions/TypeExtensions.cs | 6 ++++++ src/Generator/Generators/CSharp/CSharpSources.cs | 3 ++- src/Generator/Passes/TrimSpecializationsPass.cs | 5 ++--- tests/NamespacesDerived/NamespacesDerived.Gen.cs | 10 +++++++++- tests/NamespacesDerived/NamespacesDerived.h | 9 ++++++++- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/Generator/Extensions/TypeExtensions.cs b/src/Generator/Extensions/TypeExtensions.cs index 12200fe559..df28b169dd 100644 --- a/src/Generator/Extensions/TypeExtensions.cs +++ b/src/Generator/Extensions/TypeExtensions.cs @@ -8,6 +8,9 @@ public static class TypeExtensions { public static int GetWidth(this Type type, ParserTargetInfo targetInfo) { + if (type is TemplateSpecializationType specializationType) + type = specializationType.Desugared.Type; + if (type.IsPrimitiveType(out var primitiveType)) return (int)primitiveType.GetInfo(targetInfo, out _).Width; @@ -26,6 +29,9 @@ public static int GetWidth(this Type type, ParserTargetInfo targetInfo) public static int GetAlignment(this Type type, ParserTargetInfo targetInfo) { + if (type is TemplateSpecializationType specializationType) + type = specializationType.Desugared.Type; + if (type.IsPrimitiveType(out var primitiveType)) return (int)primitiveType.GetInfo(targetInfo, out _).Alignment; diff --git a/src/Generator/Generators/CSharp/CSharpSources.cs b/src/Generator/Generators/CSharp/CSharpSources.cs index 390ce327ab..d13b03246c 100644 --- a/src/Generator/Generators/CSharp/CSharpSources.cs +++ b/src/Generator/Generators/CSharp/CSharpSources.cs @@ -128,7 +128,8 @@ group s by s.TemplatedDecl.TemplatedClass into template var declarationContexts = new Stack(); while (!(declContext is TranslationUnit)) { - declarationContexts.Push(declContext); + if (!(declContext is Namespace @namespace) || !@namespace.IsInline) + declarationContexts.Push(declContext); declContext = declContext.Namespace; } diff --git a/src/Generator/Passes/TrimSpecializationsPass.cs b/src/Generator/Passes/TrimSpecializationsPass.cs index b79fe63d5e..3c0df01920 100644 --- a/src/Generator/Passes/TrimSpecializationsPass.cs +++ b/src/Generator/Passes/TrimSpecializationsPass.cs @@ -138,8 +138,7 @@ private void CleanSpecializations(Class template) template.Specializations.All(s => s.Ignore)) template.ExplicitlyIgnore(); - if (template.Fields.Any(f => f.Type.Desugar() is TemplateParameterType)) - MoveExternalSpecializations(template); + TryMoveExternalSpecializations(template); } /// @@ -147,7 +146,7 @@ private void CleanSpecializations(Class template) /// the library their template is located in, to the module of said external types. /// /// The template to check for external specializations. - private static void MoveExternalSpecializations(Class template) + private static void TryMoveExternalSpecializations(Class template) { for (int i = template.Specializations.Count - 1; i >= 0; i--) { diff --git a/tests/NamespacesDerived/NamespacesDerived.Gen.cs b/tests/NamespacesDerived/NamespacesDerived.Gen.cs index 91267cebcf..224e6de760 100644 --- a/tests/NamespacesDerived/NamespacesDerived.Gen.cs +++ b/tests/NamespacesDerived/NamespacesDerived.Gen.cs @@ -1,4 +1,6 @@ using System.IO; +using System.Linq; +using CppSharp.AST; using CppSharp.Generators; using CppSharp.Utils; @@ -28,9 +30,15 @@ public override void Setup(Driver driver) driver.Options.Modules[1].Dependencies.Add(module); } - public override void Preprocess(Driver driver, AST.ASTContext ctx) + public override void Preprocess(Driver driver, ASTContext ctx) { ctx.IgnoreClassWithName("Ignored"); + // operator= for this type isn't necessary for testing + // while also requiring a large amount of C++ to get valid symbols; better ignore + foreach (Method @operator in ctx.FindCompleteClass("StdFields").Operators) + { + @operator.ExplicitlyIgnore(); + } } } diff --git a/tests/NamespacesDerived/NamespacesDerived.h b/tests/NamespacesDerived/NamespacesDerived.h index b1e8e7bfa4..5d14ebe2bb 100644 --- a/tests/NamespacesDerived/NamespacesDerived.h +++ b/tests/NamespacesDerived/NamespacesDerived.h @@ -2,6 +2,7 @@ #include "../NamespacesBase/NamespacesBase.h" #include "Independent.h" #include +#include // Namespace clashes with NamespacesBase.OverlappingNamespace // Test whether qualified names turn out right. @@ -105,7 +106,13 @@ class CustomAllocator class DLL_API Ignored { private: - std::basic_string, CustomAllocator> f; + std::basic_string, CustomAllocator> customAllocatedString; +}; + +class DLL_API StdFields +{ +private: + std::vector> customAllocatedVector; }; DLL_API bool operator<<(const Base& b, const char* str);