Skip to content

Commit 0919026

Browse files
committed
Generate valid C# for internal fields of type external specialization
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent c8c415b commit 0919026

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

src/Generator/Extensions/TypeExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public static class TypeExtensions
88
{
99
public static int GetWidth(this Type type, ParserTargetInfo targetInfo)
1010
{
11+
if (type is TemplateSpecializationType specializationType)
12+
type = specializationType.Desugared.Type;
13+
1114
if (type.IsPrimitiveType(out var primitiveType))
1215
return (int)primitiveType.GetInfo(targetInfo, out _).Width;
1316

@@ -26,6 +29,9 @@ public static int GetWidth(this Type type, ParserTargetInfo targetInfo)
2629

2730
public static int GetAlignment(this Type type, ParserTargetInfo targetInfo)
2831
{
32+
if (type is TemplateSpecializationType specializationType)
33+
type = specializationType.Desugared.Type;
34+
2935
if (type.IsPrimitiveType(out var primitiveType))
3036
return (int)primitiveType.GetInfo(targetInfo, out _).Alignment;
3137

src/Generator/Generators/CSharp/CSharpSources.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ group s by s.TemplatedDecl.TemplatedClass into template
128128
var declarationContexts = new Stack<DeclarationContext>();
129129
while (!(declContext is TranslationUnit))
130130
{
131-
declarationContexts.Push(declContext);
131+
if (!(declContext is Namespace @namespace) || !@namespace.IsInline)
132+
declarationContexts.Push(declContext);
132133
declContext = declContext.Namespace;
133134
}
134135

src/Generator/Passes/TrimSpecializationsPass.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,15 @@ private void CleanSpecializations(Class template)
138138
template.Specializations.All(s => s.Ignore))
139139
template.ExplicitlyIgnore();
140140

141-
if (template.Fields.Any(f => f.Type.Desugar() is TemplateParameterType))
142-
MoveExternalSpecializations(template);
141+
TryMoveExternalSpecializations(template);
143142
}
144143

145144
/// <summary>
146145
/// Moves specializations which use in their arguments types located outside
147146
/// the library their template is located in, to the module of said external types.
148147
/// </summary>
149148
/// <param name="template">The template to check for external specializations.</param>
150-
private static void MoveExternalSpecializations(Class template)
149+
private static void TryMoveExternalSpecializations(Class template)
151150
{
152151
for (int i = template.Specializations.Count - 1; i >= 0; i--)
153152
{

tests/NamespacesDerived/NamespacesDerived.Gen.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.IO;
2+
using System.Linq;
3+
using CppSharp.AST;
24
using CppSharp.Generators;
35
using CppSharp.Utils;
46

@@ -28,9 +30,15 @@ public override void Setup(Driver driver)
2830
driver.Options.Modules[1].Dependencies.Add(module);
2931
}
3032

31-
public override void Preprocess(Driver driver, AST.ASTContext ctx)
33+
public override void Preprocess(Driver driver, ASTContext ctx)
3234
{
3335
ctx.IgnoreClassWithName("Ignored");
36+
// operator= for this type isn't necessary for testing
37+
// while also requiring a large amount of C++ to get valid symbols; better ignore
38+
foreach (Method @operator in ctx.FindCompleteClass("StdFields").Operators)
39+
{
40+
@operator.ExplicitlyIgnore();
41+
}
3442
}
3543
}
3644

tests/NamespacesDerived/NamespacesDerived.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../NamespacesBase/NamespacesBase.h"
33
#include "Independent.h"
44
#include <string>
5+
#include <vector>
56

67
// Namespace clashes with NamespacesBase.OverlappingNamespace
78
// Test whether qualified names turn out right.
@@ -105,7 +106,13 @@ class CustomAllocator
105106
class DLL_API Ignored
106107
{
107108
private:
108-
std::basic_string<char, std::char_traits<char>, CustomAllocator<char>> f;
109+
std::basic_string<char, std::char_traits<char>, CustomAllocator<char>> customAllocatedString;
110+
};
111+
112+
class DLL_API StdFields
113+
{
114+
private:
115+
std::vector<unsigned int, CustomAllocator<unsigned int>> customAllocatedVector;
109116
};
110117

111118
DLL_API bool operator<<(const Base& b, const char* str);

0 commit comments

Comments
 (0)