Skip to content

Commit

Permalink
Generate valid C# for internal fields of type external specialization
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Oct 29, 2021
1 parent c8c415b commit 0919026
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/Generator/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ group s by s.TemplatedDecl.TemplatedClass into template
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;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Generator/Passes/TrimSpecializationsPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,15 @@ 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);
}

/// <summary>
/// Moves specializations which use in their arguments types located outside
/// 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--)
{
Expand Down
10 changes: 9 additions & 1 deletion tests/NamespacesDerived/NamespacesDerived.Gen.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.IO;
using System.Linq;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Utils;

Expand Down Expand Up @@ -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();
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion tests/NamespacesDerived/NamespacesDerived.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -105,7 +106,13 @@ class CustomAllocator
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);
Expand Down

0 comments on commit 0919026

Please sign in to comment.