Skip to content

Commit

Permalink
Fixed a crash when a constructor takes a template or specialisation.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information
ddobrev committed Dec 24, 2017
1 parent 6dec97f commit ae9eede
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
22 changes: 22 additions & 0 deletions src/AST/ClassExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,28 @@ private static IEnumerable<Class> GetSpecializedClassesOf(this Class dependentCl
c => c.Name == dependentClass.Name)).ToList();
}

public static Class GetInterface(this Class @class)
{
var specialization = @class as ClassTemplateSpecialization;
Class @interface = null;
if (specialization == null)
{
@interface = @class.Namespace.Classes.Find(
c => c.OriginalClass == @class && c.IsInterface);
}
else
{
Class template = specialization.TemplatedDecl.TemplatedClass;
Class templatedInterface = @class.Namespace.Classes.Find(
c => c.OriginalClass == template && c.IsInterface);
if (templatedInterface != null)
@interface = templatedInterface.Specializations.FirstOrDefault(
s => s.OriginalClass == specialization && s.IsInterface);
}

return @interface;
}

public static bool HasDependentValueFieldInLayout(this Class @class)
{
if (@class.Fields.Any(f => IsValueDependent(f.Type)))
Expand Down
2 changes: 1 addition & 1 deletion src/Generator/Generators/CSharp/CSharpSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2595,7 +2595,7 @@ private void GenerateOperator(Method method, QualifiedType returnType)
Class paramClass;
Class @interface = null;
if (paramType.TryGetClass(out paramClass))
@interface = paramClass.Namespace.Classes.Find(c => c.OriginalClass == paramClass);
@interface = paramClass.GetInterface();

var paramName = string.Format("{0}{1}",
method.Parameters[0].Type.IsPrimitiveTypeConvertibleToRef() ?
Expand Down
17 changes: 1 addition & 16 deletions src/Generator/Passes/ParamTypeToInterfacePass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,7 @@ private static void ChangeToInterfaceType(ref QualifiedType type)
if (!finalType.TryGetClass(out @class))
return;

var specialization = @class as ClassTemplateSpecialization;
Class @interface = null;
if (specialization == null)
{
@interface = @class.Namespace.Classes.Find(
c => c.OriginalClass == @class && c.IsInterface);
}
else
{
Class template = specialization.TemplatedDecl.TemplatedClass;
Class templatedInterface = @class.Namespace.Classes.Find(
c => c.OriginalClass == template && c.IsInterface);
if (templatedInterface != null)
@interface = templatedInterface.Specializations.FirstOrDefault(
s => s.OriginalClass == specialization);
}
Class @interface = @class.GetInterface();
if (@interface == null)
return;

Expand Down
12 changes: 12 additions & 0 deletions tests/CSharp/CSharpTemplates.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class DLL_API DependentValueFields : public Base<T>
T field;
};
DependentValueFields();
DependentValueFields(IndependentFields<T> i);
~DependentValueFields();
DependentValueFields& returnInjectedClass();
DependentValueFields returnValue();
Expand All @@ -163,6 +164,11 @@ DependentValueFields<T>::~DependentValueFields()
{
}

template <typename T>
DependentValueFields<T>::DependentValueFields(IndependentFields<T> i)
{
}

template <typename T>
T DependentValueFields<T>::getDependentValue()
{
Expand Down Expand Up @@ -404,6 +410,7 @@ class VirtualTemplate
{
public:
VirtualTemplate();
VirtualTemplate(IndependentFields<int> i);
VirtualTemplate(OptionalTemplateArgs<T> optionalTemplateArgs);
virtual ~VirtualTemplate();
virtual int function();
Expand All @@ -415,6 +422,11 @@ VirtualTemplate<T>::VirtualTemplate()
{
}

template <typename T>
VirtualTemplate<T>::VirtualTemplate(IndependentFields<int> i)
{
}

template <typename T>
VirtualTemplate<T>::VirtualTemplate(OptionalTemplateArgs<T> optionalTemplateArgs)
{
Expand Down

0 comments on commit ae9eede

Please sign in to comment.