Permalink
Browse files

Made the generated exception for templates more explanatory.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
  • Loading branch information...
ddobrev committed Jul 15, 2017
1 parent 1084dcc commit 6918d9d76906b53a296bb1a858e27a5c3259b766
@@ -280,7 +280,7 @@ public override bool VisitClassDecl(Class @class)
var typeMaps = new List<System.Type>();
var keys = new List<string>();
// disable the type maps, if any, for this class because of copy ctors, operators and others
CSharpSourcesExtensions.DisableTypeMap(@class, typeMaps, keys, Context);
this.DisableTypeMap(@class, typeMaps, keys);
PushBlock(BlockKind.Class);
GenerateDeclarationCommon(@class);
@@ -8,14 +8,14 @@ namespace CppSharp.Generators.CSharp
{
public static class CSharpSourcesExtensions
{
public static void DisableTypeMap(Class @class,
List<System.Type> typeMaps, List<string> keys, BindingContext context)
public static void DisableTypeMap(this CSharpSources gen, Class @class,
List<System.Type> typeMaps, List<string> keys)
{
var mapped = @class.OriginalClass ?? @class;
DisableSingleTypeMap(mapped, typeMaps, keys, context);
DisableSingleTypeMap(mapped, typeMaps, keys, gen.Context);
if (mapped.IsDependent)
foreach (var specialization in mapped.Specializations)
DisableSingleTypeMap(specialization, typeMaps, keys, context);
DisableSingleTypeMap(specialization, typeMaps, keys, gen.Context);
}
public static void GenerateNativeConstructorsByValue(
@@ -38,7 +38,7 @@ public static class CSharpSourcesExtensions
if (@class.Fields.Any(f => f.Type.Desugar() is TemplateParameterType))
{
foreach (var parameter in @class.TemplateParameters)
gen.WriteLine("var __{0} = typeof({0});", parameter.Name);
gen.WriteLine($"var __{parameter.Name} = typeof({parameter.Name});");
foreach (var specialization in @class.Specializations.Where(s => !s.Ignore))
{
@@ -51,7 +51,7 @@ public static class CSharpSourcesExtensions
gen.WriteLine("return;");
gen.WriteCloseBraceIndent();
}
gen.WriteLine("throw new global::System.InvalidOperationException();");
ThrowException(gen, @class);
}
else
{
@@ -84,7 +84,7 @@ public static class CSharpSourcesExtensions
gen.WriteLine("return;");
gen.WriteCloseBraceIndent();
}
gen.WriteLine("throw new global::System.InvalidOperationException();");
ThrowException(gen, @class);
}
else
{
@@ -122,5 +122,18 @@ public static class CSharpSourcesExtensions
@class.TemplateParameters[i].Name,
specialization.Arguments[i].Type.Type.Desugar()))));
}
private static void ThrowException(CSharpSources gen, Class @class)
{
var typePrinter = new CSharpTypePrinter(gen.Context);
var supportedTypes = string.Join(", ",
@class.Specializations.Where(s => !s.Ignore).Select(s => $@"<{string.Join(", ",
s.Arguments.Select(a => typePrinter.VisitTemplateArgument(a)))}>"));
var typeArguments = string.Join(", ", @class.TemplateParameters.Select(p => p.Name));
var managedTypes = string.Join(", ", @class.TemplateParameters.Select(p => $"typeof({p.Name}).FullName"));
gen.WriteLine($"throw new ArgumentOutOfRangeException(\"{typeArguments}\", "
+ $@"string.Join("", "", new[] {{ {managedTypes} }}), "
+ $"\"{@class.Visit(typePrinter)}<{typeArguments}> maps a C++ template class and therefore it only supports a limited set of types and their subclasses: {supportedTypes}.\");");
}
}
}
@@ -567,7 +567,7 @@ public override TypePrinterResult VisitClassDecl(Class @class)
return $"{VisitClassDecl(specialization)}<{args}>";
}
private TypePrinterResult VisitTemplateArgument(TemplateArgument a)
public TypePrinterResult VisitTemplateArgument(TemplateArgument a)
{
if (a.Type.Type == null)
return a.Integral.ToString(CultureInfo.InvariantCulture);

0 comments on commit 6918d9d

Please sign in to comment.