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 <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Oct 29, 2021
1 parent c8c415b commit d35883b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 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
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
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 d35883b

Please sign in to comment.