Fix error "can not find instance for typeclass"

This commit is contained in:
Bogan Voloshin 2020-11-10 09:24:23 +03:00
parent 554a08b844
commit 8054f57e92
6 changed files with 31 additions and 14 deletions

View file

@ -113,10 +113,9 @@ namespace SyntaxVisitors.TypeclassVisitors
instanceDeclTranslated.attributes = instanceDeclaration.attributes;
AddAttribute(
instanceDeclTranslated, "__TypeclassInstanceAttribute",
new expression_list(new string_const(TypeclassRestrictionToString(instanceName))));
AddAttribute(instanceDeclTranslated, "__TypeclassAttribute",
new expression_list(new string_const(TypeclassRestrictionToString(
(originalTypeclass.Parent as type_declaration).type_name as typeclass_restriction))));
string_consts(
TypeclassRestrictionToString(instanceName),
((originalTypeclass.Parent as type_declaration).type_name as typeclass_restriction).name));
Replace(instanceDeclaration, instanceDeclTranslated);
visit(instanceDeclTranslated);
@ -205,7 +204,7 @@ namespace SyntaxVisitors.TypeclassVisitors
typeclassInterfaceDecl.attributes = typeclassDeclaration.attributes;
AddAttribute(
typeclassInterfaceDecl, "__TypeclassAttribute",
new expression_list(new string_const(TypeclassRestrictionToString(typeclassName))));
string_consts(TypeclassRestrictionToString(typeclassName)));
// Creating class
@ -344,7 +343,7 @@ namespace SyntaxVisitors.TypeclassVisitors
typeclassDeclTranslated.attributes = typeclassDeclaration.attributes;
AddAttribute(
typeclassDeclTranslated, "__TypeclassAttribute",
new expression_list(new string_const(TypeclassRestrictionToString(typeclassName))));
string_consts(TypeclassRestrictionToString(typeclassName)));
Replace(typeclassDeclaration, typeclassDeclTranslated);
UpperNodeAs<type_declarations>().InsertBefore(typeclassDeclTranslated, typeclassInterfaceDecl);
@ -436,7 +435,7 @@ namespace SyntaxVisitors.TypeclassVisitors
var newName = TypeclassRestrctionToTemplateName(typeclassWhere.restriction.name, typeclassWhere.restriction.restriction_args);
AddAttribute(
newName, "__TypeclassGenericParameter",
new expression_list(new string_const(GetInstanceSingletonName(newName.name))));
string_consts(GetInstanceSingletonName(newName.name), typeclassWhere.restriction.name));
additionalTemplateArgs.Add(newName);
// Create name for template that replaces typeclass(for ex. SumTC)
@ -615,6 +614,13 @@ namespace SyntaxVisitors.TypeclassVisitors
return RestrictionsToIdentList(restriction_args).idents.Aggregate(
new ident(name), (x, y) => x.name + y.name);
}
private static expression_list string_consts(params string[] strs)
{
return new expression_list(
strs.Select(x => (new string_const(x)) as expression).ToList());
}
}

View file

@ -44,7 +44,16 @@ namespace PascalABCCompiler.TreeConverter
public static string GetTypeclassName(type_node ctn)
{
return ctn?.attributes?.First(x => x.AttributeType.name == "__TypeclassAttribute")?.Arguments[0]?.value?.ToString();
var att = ctn?.attributes;
string res = null;
res = att.FirstOrDefault(x => x.AttributeType.name == "__TypeclassAttribute")?.Arguments[0]?.value?.ToString();
if (res == null)
res = att.FirstOrDefault(x => x.AttributeType.name == "__TypeclassGenericParameterAttribute")?.Arguments[1]?.value?.ToString();
if (res == null)
res = att.FirstOrDefault(x => x.AttributeType.name == "__TypeclassInstanceAttribute")?.Arguments[1]?.value?.ToString();
return res;
}
public static string ResugarMethodName(string name)

View file

@ -1062,11 +1062,11 @@ namespace PascalABCCompiler.TreeRealization
{
if (appropriateInstances.Count() == 0)
{
throw new SimpleSemanticError(loc, "NO_INSTANCES_FOR_TYPECLASS_{0}", null/*TypeclassHelper.GetTypeclassName(tc)*/);
throw new SimpleSemanticError(loc, "NO_INSTANCES_FOR_TYPECLASS_{0}", TypeclassHelper.GetTypeclassName(tc));
}
else
{
throw new SimpleSemanticError(loc, "TC_SEVERAL_INSTANCES_FOR_TYPECLASS_{0}", null /*TypeclassHelper.GetTypeclassName(tc)*/);
throw new SimpleSemanticError(loc, "TC_SEVERAL_INSTANCES_FOR_TYPECLASS_{0}", TypeclassHelper.GetTypeclassName(tc));
}
}
}

View file

@ -2679,7 +2679,7 @@ type
///--
__TypeclassGenericParameterAttribute = class(Attribute)
public
constructor(instanceName: string);
constructor(instanceName: string; originalTypeclassName: string);
begin
end;
end;
@ -2703,7 +2703,7 @@ type
///--
__TypeclassInstanceAttribute = class(Attribute)
public
constructor(instanceName: string);
constructor(instanceName: string; originalTypeclassName: string);
begin
end;
end;

View file

@ -1,2 +1,3 @@
%PREFIX%=SEMANTICERROR_
INSTANCE_{0}_DOES_NOT_IMPLEMENT_REQUIREMENT_{1}_OF_TYPECLASS_{2}=Instance {0} doesn't implement {1}
INSTANCE_{0}_DOES_NOT_IMPLEMENT_REQUIREMENT_{1}_OF_TYPECLASS_{2}=Instance {0} doesn't implement {1}
NO_INSTANCES_FOR_TYPECLASS_{0}=Can't infer instance for typeclass {0}, please specify instance explicitly

View file

@ -1,2 +1,3 @@
%PREFIX%=SEMANTICERROR_
INSTANCE_{0}_DOES_NOT_IMPLEMENT_REQUIREMENT_{1}_OF_TYPECLASS_{2}=Инстанс {0} не переопределяет {1}
INSTANCE_{0}_DOES_NOT_IMPLEMENT_REQUIREMENT_{1}_OF_TYPECLASS_{2}=Инстанс {0} не переопределяет {1}
NO_INSTANCES_FOR_TYPECLASS_{0}=Невозможно вывести инстанс для класса типа {0}, пожалуйста укажите его явно