Browse Source

Generate valid C# for internal fields of type external specialization

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1641/head
Dimitar Dobrev 4 years ago
parent
commit
09190268bc
  1. 6
      src/Generator/Extensions/TypeExtensions.cs
  2. 3
      src/Generator/Generators/CSharp/CSharpSources.cs
  3. 5
      src/Generator/Passes/TrimSpecializationsPass.cs
  4. 10
      tests/NamespacesDerived/NamespacesDerived.Gen.cs
  5. 9
      tests/NamespacesDerived/NamespacesDerived.h

6
src/Generator/Extensions/TypeExtensions.cs

@ -8,6 +8,9 @@ namespace CppSharp.Extensions @@ -8,6 +8,9 @@ namespace CppSharp.Extensions
{
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 @@ namespace CppSharp.Extensions @@ -26,6 +29,9 @@ namespace CppSharp.Extensions
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;

3
src/Generator/Generators/CSharp/CSharpSources.cs

@ -128,7 +128,8 @@ namespace CppSharp.Generators.CSharp @@ -128,7 +128,8 @@ namespace CppSharp.Generators.CSharp
var declarationContexts = new Stack<DeclarationContext>();
while (!(declContext is TranslationUnit))
{
declarationContexts.Push(declContext);
if (!(declContext is Namespace @namespace) || !@namespace.IsInline)
declarationContexts.Push(declContext);
declContext = declContext.Namespace;
}

5
src/Generator/Passes/TrimSpecializationsPass.cs

@ -138,8 +138,7 @@ namespace CppSharp.Passes @@ -138,8 +138,7 @@ namespace CppSharp.Passes
template.Specializations.All(s => s.Ignore))
template.ExplicitlyIgnore();
if (template.Fields.Any(f => f.Type.Desugar() is TemplateParameterType))
MoveExternalSpecializations(template);
TryMoveExternalSpecializations(template);
}
/// <summary>
@ -147,7 +146,7 @@ namespace CppSharp.Passes @@ -147,7 +146,7 @@ namespace CppSharp.Passes
/// the library their template is located in, to the module of said external types.
/// </summary>
/// <param name="template">The template to check for external specializations.</param>
private static void MoveExternalSpecializations(Class template)
private static void TryMoveExternalSpecializations(Class template)
{
for (int i = template.Specializations.Count - 1; i >= 0; i--)
{

10
tests/NamespacesDerived/NamespacesDerived.Gen.cs

@ -1,4 +1,6 @@ @@ -1,4 +1,6 @@
using System.IO;
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Utils;
@ -28,9 +30,15 @@ namespace CppSharp.Tests @@ -28,9 +30,15 @@ namespace CppSharp.Tests
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();
}
}
}

9
tests/NamespacesDerived/NamespacesDerived.h

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#include "../NamespacesBase/NamespacesBase.h"
#include "Independent.h"
#include <string>
#include <vector>
// Namespace clashes with NamespacesBase.OverlappingNamespace
// Test whether qualified names turn out right.
@ -105,7 +106,13 @@ public: @@ -105,7 +106,13 @@ public:
class DLL_API Ignored
{
private:
std::basic_string<char, std::char_traits<char>, CustomAllocator<char>> f;
std::basic_string<char, std::char_traits<char>, CustomAllocator<char>> customAllocatedString;
};
class DLL_API StdFields
{
private:
std::vector<unsigned int, CustomAllocator<unsigned int>> customAllocatedVector;
};
DLL_API bool operator<<(const Base& b, const char* str);

Loading…
Cancel
Save