Skip to content

Commit aab63d0

Browse files
committed
Generate valid C# for unresolvable base templates
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 69e766b commit aab63d0

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public virtual void GenerateNamespaceFunctionsAndVariables(DeclarationContext co
274274
WriteOpenBraceAndIndent();
275275

276276
PushBlock(BlockKind.InternalsClass);
277-
GenerateClassInternalHead();
277+
GenerateClassInternalHead(new Class { Name = parentName });
278278
WriteOpenBraceAndIndent();
279279

280280
// Generate all the internal function declarations.
@@ -704,7 +704,7 @@ private IEnumerable<string> GatherInternalParams(Function function, out TypePrin
704704
return @params;
705705
}
706706

707-
private void GenerateClassInternalHead(Class @class = null)
707+
private void GenerateClassInternalHead(Class @class)
708708
{
709709
Write("public ");
710710

@@ -3143,7 +3143,7 @@ public void GenerateFunctionCall(string functionName, Function function,
31433143
if (operatorParam == null)
31443144
{
31453145
WriteLine($@"fixed ({Helpers.InternalStruct}{
3146-
Helpers.GetSuffixForInternal(originalFunction.Namespace)}* __instancePtr = &{
3146+
Helpers.GetSuffixForInternal((Class) originalFunction.Namespace)}* __instancePtr = &{
31473147
Helpers.InstanceField})");
31483148
WriteOpenBraceAndIndent();
31493149
}

src/Generator/Generators/CodeGenerator.cs

+17-16
Original file line numberDiff line numberDiff line change
@@ -1303,29 +1303,30 @@ public static class Helpers
13031303
public static readonly string CreateInstanceIdentifier = Generator.GeneratedIdentifier("CreateInstance");
13041304
public static readonly string GetOrCreateInstanceIdentifier = Generator.GeneratedIdentifier("GetOrCreateInstance");
13051305

1306-
public static string GetSuffixForInternal(DeclarationContext @class)
1306+
public static string GetSuffixForInternal(Class @class)
13071307
{
1308-
if (@class == null)
1309-
return string.Empty;
1310-
1311-
Class template = null;
13121308
var specialization = @class as ClassTemplateSpecialization ??
13131309
@class.Namespace as ClassTemplateSpecialization;
1314-
if (specialization != null)
1315-
{
1316-
template = specialization.TemplatedDecl.TemplatedClass;
1317-
if (@class != specialization)
1318-
template = template.Classes.FirstOrDefault(c => c.Name == @class.Name);
1319-
}
13201310

1321-
if (template == null || !template.HasDependentValueFieldInLayout())
1311+
if (specialization == null)
13221312
return string.Empty;
13231313

1324-
if (specialization.Arguments.All(
1325-
a => a.Type.Type?.IsAddress() == true))
1326-
return "_Ptr";
1314+
Class template = specialization.TemplatedDecl.TemplatedClass;
1315+
if (@class != specialization)
1316+
template = template.Classes.FirstOrDefault(c => c.Name == @class.Name);
13271317

1328-
return GetSuffixFor(specialization);
1318+
if (template.HasDependentValueFieldInLayout())
1319+
{
1320+
if (specialization.Arguments.All(
1321+
a => a.Type.Type?.IsAddress() == true))
1322+
return "_Ptr";
1323+
return GetSuffixFor(specialization);
1324+
}
1325+
// HACK: Clang can't always resolve complex templates such as the base of std::atomic in msvc
1326+
return (from @base in @class.Bases
1327+
let suffix = GetSuffixForInternal(@base.Class)
1328+
where suffix.Length > 0
1329+
select suffix).DefaultIfEmpty(string.Empty).First();
13291330
}
13301331

13311332
public static string GetSuffixFor(Declaration decl)

tests/CSharp/CSharp.h

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include "../Tests.h"
4+
#include <atomic>
45
#include <cstdint>
56
#include <vector>
67
#include <limits>
@@ -224,6 +225,8 @@ class DLL_API Proprietor : public AbstractProprietor
224225
private:
225226
Bar::Items _items;
226227
Bar::Items _itemsByValue;
228+
std::atomic<int> atomicPrimitive;
229+
std::atomic<SmallPOD> atomicCustom;
227230
};
228231

229232
class DLL_API ComplexType

0 commit comments

Comments
 (0)