Skip to content

Commit

Permalink
Generate valid C# for unresolvable base templates
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <[email protected]>
  • Loading branch information
ddobrev committed Dec 20, 2021
1 parent 69e766b commit aab63d0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public virtual void GenerateNamespaceFunctionsAndVariables(DeclarationContext co
WriteOpenBraceAndIndent();

PushBlock(BlockKind.InternalsClass);
GenerateClassInternalHead();
GenerateClassInternalHead(new Class { Name = parentName });
WriteOpenBraceAndIndent();

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

private void GenerateClassInternalHead(Class @class = null)
private void GenerateClassInternalHead(Class @class)
{
Write("public ");

Expand Down Expand Up @@ -3143,7 +3143,7 @@ public void GenerateFunctionCall(string functionName, Function function,
if (operatorParam == null)
{
WriteLine($@"fixed ({Helpers.InternalStruct}{
Helpers.GetSuffixForInternal(originalFunction.Namespace)}* __instancePtr = &{
Helpers.GetSuffixForInternal((Class) originalFunction.Namespace)}* __instancePtr = &{
Helpers.InstanceField})");
WriteOpenBraceAndIndent();
}
Expand Down
33 changes: 17 additions & 16 deletions src/Generator/Generators/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,29 +1303,30 @@ public static class Helpers
public static readonly string CreateInstanceIdentifier = Generator.GeneratedIdentifier("CreateInstance");
public static readonly string GetOrCreateInstanceIdentifier = Generator.GeneratedIdentifier("GetOrCreateInstance");

public static string GetSuffixForInternal(DeclarationContext @class)
public static string GetSuffixForInternal(Class @class)
{
if (@class == null)
return string.Empty;

Class template = null;
var specialization = @class as ClassTemplateSpecialization ??
@class.Namespace as ClassTemplateSpecialization;
if (specialization != null)
{
template = specialization.TemplatedDecl.TemplatedClass;
if (@class != specialization)
template = template.Classes.FirstOrDefault(c => c.Name == @class.Name);
}

if (template == null || !template.HasDependentValueFieldInLayout())
if (specialization == null)
return string.Empty;

if (specialization.Arguments.All(
a => a.Type.Type?.IsAddress() == true))
return "_Ptr";
Class template = specialization.TemplatedDecl.TemplatedClass;
if (@class != specialization)
template = template.Classes.FirstOrDefault(c => c.Name == @class.Name);

return GetSuffixFor(specialization);
if (template.HasDependentValueFieldInLayout())
{
if (specialization.Arguments.All(
a => a.Type.Type?.IsAddress() == true))
return "_Ptr";
return GetSuffixFor(specialization);
}
// HACK: Clang can't always resolve complex templates such as the base of std::atomic in msvc
return (from @base in @class.Bases
let suffix = GetSuffixForInternal(@base.Class)
where suffix.Length > 0
select suffix).DefaultIfEmpty(string.Empty).First();
}

public static string GetSuffixFor(Declaration decl)
Expand Down
3 changes: 3 additions & 0 deletions tests/CSharp/CSharp.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "../Tests.h"
#include <atomic>
#include <cstdint>
#include <vector>
#include <limits>
Expand Down Expand Up @@ -224,6 +225,8 @@ class DLL_API Proprietor : public AbstractProprietor
private:
Bar::Items _items;
Bar::Items _itemsByValue;
std::atomic<int> atomicPrimitive;
std::atomic<SmallPOD> atomicCustom;
};

class DLL_API ComplexType
Expand Down

0 comments on commit aab63d0

Please sign in to comment.