Permalink
Browse files

Bound the supported specialisations with generic syntax in the genera…

…ted C#. (#880)

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information...
ddobrev committed Jul 13, 2017
1 parent 49e9446 commit 9c0f80f69d505654fce384b8b1214d3af96974ae
@@ -185,5 +185,21 @@ public static IEnumerable<TranslationUnit> GetGenerated(this IEnumerable<Transla
{
return units.Where(u => u.IsGenerated && (u.HasDeclarations || u.IsSystemHeader) && u.IsValid);
}
public static List<ClassTemplateSpecialization> GetSpecializationsToGenerate(
this Class classTemplate)
{
if (classTemplate.Fields.Any(
f => f.Type.Desugar() is TemplateParameterType))
return classTemplate.Specializations;
var specializations = new List<ClassTemplateSpecialization>();
var specialization = classTemplate.Specializations.FirstOrDefault(s => !s.Ignore);
if (specialization == null)
specializations.Add(classTemplate.Specializations[0]);
else
specializations.Add(specialization);
return specializations;
}
}
}
@@ -27,7 +27,8 @@ public enum ParameterKind
Regular,
IndirectReturnType,
OperatorParameter,
ImplicitDestructorParameter
ImplicitDestructorParameter,
Extension
}
public class Parameter : Declaration, ITypedDecl
@@ -11,7 +11,7 @@ public static class FunctionExtensions
{
var @params = new List<Parameter>();
var method = function as Method;
var method = (function.OriginalFunction ?? function) as Method;
var isInstanceMethod = method != null && !method.IsStatic;
var pointer = new QualifiedType(new PointerType(new QualifiedType(new BuiltinType(PrimitiveType.Void))));
@@ -36,7 +36,8 @@ public static class FunctionExtensions
}
var i = 0;
foreach (var param in function.Parameters.Where(p => p.Kind != ParameterKind.OperatorParameter))
foreach (var param in function.Parameters.Where(
p => p.Kind != ParameterKind.OperatorParameter && p.Kind != ParameterKind.Extension))
{
@params.Add(new Parameter
{
@@ -821,10 +821,10 @@ public override bool Equals(object obj)
var type = obj as TemplateParameterType;
if (type == null) return false;
return Parameter.Equals(type.Parameter)
&& Depth.Equals(type.Depth)
&& Index.Equals(type.Index)
&& IsParameterPack.Equals(type.IsParameterPack);
return Parameter == type.Parameter
&& Depth == type.Depth
&& Index == type.Index
&& IsParameterPack == type.IsParameterPack;
}
public override int GetHashCode()
@@ -185,27 +185,39 @@ public static bool TryGetEnum(this Type t, out Enumeration @enum)
return @enum != null;
}
public static Type Desugar(this Type t)
public static Type Desugar(this Type t, bool resolveTemplateSubstitution = true)
{
var typeDef = t as TypedefType;
if (typeDef != null)
{
var decl = typeDef.Declaration.Type;
if (decl != null)
return decl.Desugar();
return decl.Desugar(resolveTemplateSubstitution);
}
var substType = t as TemplateParameterSubstitutionType;
if (substType != null)
if (resolveTemplateSubstitution)
{
var replacement = substType.Replacement.Type;
if (replacement != null)
return replacement.Desugar();
var substType = t as TemplateParameterSubstitutionType;
if (substType != null)
{
var replacement = substType.Replacement.Type;
if (replacement != null)
return replacement.Desugar(resolveTemplateSubstitution);
}
}
var injectedType = t as InjectedClassNameType;
if (injectedType != null)
{
if (injectedType.InjectedSpecializationType.Type != null)
return injectedType.InjectedSpecializationType.Type.Desugar(
resolveTemplateSubstitution);
return new TagType(injectedType.Class);
}
var attributedType = t as AttributedType;
if (attributedType != null)
return attributedType.Equivalent.Type.Desugar();
return attributedType.Equivalent.Type.Desugar(resolveTemplateSubstitution);
return t;
}
@@ -322,5 +334,18 @@ public static bool ResolvesTo(this QualifiedType type, QualifiedType other)
}
return left.Equals(right);
}
public static bool IsDependentPointer(this Type type)
{
var desugared = type.Desugar();
if (desugared.IsAddress())
{
var pointee = desugared.GetFinalPointee().Desugar();
return pointee.IsDependent
&& !(pointee is TemplateSpecializationType)
&& !(pointee is InjectedClassNameType);
}
return false;
}
}
}
@@ -335,6 +335,9 @@ public void SetupPasses(ILibrary library)
TranslationUnitPasses.AddPass(new GetterSetterToPropertyPass());
TranslationUnitPasses.AddPass(new StripUnusedSystemTypesPass());
if (Options.IsCSharpGenerator)
TranslationUnitPasses.AddPass(new SpecializationMethodsWithDependentPointersPass());
if (Options.GeneratorKind == GeneratorKind.CLI ||
Options.GeneratorKind == GeneratorKind.CSharp)
TranslationUnitPasses.RenameDeclsUpperCase(RenameTargets.Any &~ RenameTargets.Parameter);
@@ -834,7 +834,8 @@ public void GenerateEnum(Enumeration @enum)
var typeName = TypePrinter.VisitPrimitiveType(@enum.BuiltinType.Type,
new TypeQualifiers());
if (@enum.BuiltinType.Type != PrimitiveType.Int)
if (@enum.BuiltinType.Type != PrimitiveType.Int &&
@enum.BuiltinType.Type != PrimitiveType.Null)
Write(" : {0}", typeName);
NewLine();
Oops, something went wrong.

0 comments on commit 9c0f80f

Please sign in to comment.