Add error for undefined restrictions in instance

This commit is contained in:
Bogan Voloshin 2020-11-14 16:01:20 +03:00
parent 4fb04698be
commit dc93588518
6 changed files with 37 additions and 5 deletions

View file

@ -0,0 +1,8 @@
type
tc1[T] = typeclass end;
I1=interface end;
tc1[I1] = instance end;//не должно компилироваться
begin
end.

View file

@ -26,12 +26,27 @@ namespace PascalABCCompiler.TreeConverter
return isInstance.HasValue && isInstance.Value;
}
public static common_type_node GetTopClass(compilation_context context)
{
return (context.CurrentScope.TopScope as SymbolTable.ClassScope)?.class_type;
}
public static bool IsTypeclass(type_node ctn)
{
var isTypeclass = ctn?.attributes?.Any(x => x.AttributeType.name == "__TypeclassAttribute");
return isTypeclass.HasValue && isTypeclass.Value;
}
public static bool IsInInstanceContext(compilation_context context)
{
return IsInstance(GetTopClass(context));
}
public static bool IsInTypeclassContext(compilation_context context)
{
return IsTypeclass(GetTopClass(context));
}
public static string OperatorName(string op)
{
return "$typeclass" + op;

View file

@ -3757,8 +3757,16 @@ namespace PascalABCCompiler.TreeConverter
{
cmn.polymorphic_state = SemanticTree.polymorphic_state.ps_virtual;
cmn.overrided_method = FindMethodToOverride(cmn);
if (cmn.overrided_method == null)
AddError(cmn.loc, "NO_METHOD_TO_OVERRIDE");
{
var instance = TypeclassHelper.GetTopClass(this);
if (TypeclassHelper.IsInstance(instance))
AddError(cmn.loc, "NO_RESTRICTION_{0}_IN_TYPECLASS_{1}", cmn.name, TypeclassHelper.GetTypeclassName(instance));
else
AddError(cmn.loc, "NO_METHOD_TO_OVERRIDE");
}
cmn.SetName(cmn.overrided_method.name);
}

View file

@ -13949,8 +13949,7 @@ namespace PascalABCCompiler.TreeConverter
if (ccfn.return_value_type is undefined_type)
{
//(voloshin) Typeclasses Пока override не реализован для короких функций, короткие функции не будут поддерживаться в классах типах
var cs = (context.CurrentScope.TopScope as SymbolTable.ClassScope)?.class_type;
if (TypeclassHelper.IsInstance(cs))
if (TypeclassHelper.IsInInstanceContext(context))
{
AddError(get_location(_procedure_attributes_list.proc_attributes[i]), "SHORT_FUNCTIONS_NOT_ALLOWED_IN_TYPECLASSES");
}

View file

@ -1,4 +1,5 @@
%PREFIX%=SEMANTICERROR_
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
SHORT_FUNCTIONS_NOT_ALLOWED_IN_TYPECLASSES=It's not allowed to use short functions with typeclasses
SHORT_FUNCTIONS_NOT_ALLOWED_IN_TYPECLASSES=It's not allowed to use short functions with typeclasses
NO_RESTRICTION_{0}_IN_TYPECLASS_{1}=There is no restriction {0} in typeclass {1}

View file

@ -1,4 +1,5 @@
%PREFIX%=SEMANTICERROR_
INSTANCE_{0}_DOES_NOT_IMPLEMENT_REQUIREMENT_{1}_OF_TYPECLASS_{2}=Инстанс {0} не переопределяет {1}
NO_INSTANCES_FOR_TYPECLASS_{0}=Невозможно вывести инстанс для класса типа {0}, пожалуйста укажите его явно
SHORT_FUNCTIONS_NOT_ALLOWED_IN_TYPECLASSES=Короткие функции нельзя использовать с классами типов
SHORT_FUNCTIONS_NOT_ALLOWED_IN_TYPECLASSES=Короткие функции нельзя использовать с классами типов
NO_RESTRICTION_{0}_IN_TYPECLASS_{1}=Нет ограничения {0} в классе типов {1}