diff --git a/Compiler/FrontEnd/SCodeCheck.mo b/Compiler/FrontEnd/SCodeCheck.mo index a3418d098bc..488b3dd4a59 100644 --- a/Compiler/FrontEnd/SCodeCheck.mo +++ b/Compiler/FrontEnd/SCodeCheck.mo @@ -41,11 +41,12 @@ encapsulated package SCodeCheck public import Absyn; public import SCode; public import SCodeEnv; -public import SCodeInst; +public import InstTypes; protected import Config; protected import Dump; protected import Error; +protected import InstUtil; protected import List; protected import Util; protected import SCodeDump; @@ -591,8 +592,8 @@ algorithm end checkIdentNotEqTypeName; public function checkComponentsEqual - input SCodeInst.Component inComponent1; - input SCodeInst.Component inComponent2; + input InstTypes.Component inComponent1; + input InstTypes.Component inComponent2; algorithm _ := match(inComponent1, inComponent2) case (_, _) @@ -604,4 +605,48 @@ algorithm end match; end checkComponentsEqual; +public function checkInstanceRestriction + input SCodeEnv.Item inItem; + input InstTypes.Prefix inPrefix; + input Absyn.Info inInfo; +algorithm + _ := matchcontinue(inItem, inPrefix, inInfo) + local + SCode.Restriction res; + String pre_str, res_str; + + case (SCodeEnv.CLASS(cls = SCode.CLASS(restriction = res)), _, _) + equation + true = isInstantiableClassRestriction(res); + then + (); + + case (SCodeEnv.CLASS(cls = SCode.CLASS(restriction = res)), _, _) + equation + res_str = SCodeDump.restrictionStringPP(res); + pre_str = InstUtil.printPrefix(inPrefix); + Error.addSourceMessage(Error.INVALID_CLASS_RESTRICTION, + {res_str, pre_str}, inInfo); + then + fail(); + + end matchcontinue; +end checkInstanceRestriction; + +protected function isInstantiableClassRestriction + input SCode.Restriction inRestriction; + output Boolean outIsInstantiable; +algorithm + outIsInstantiable := match(inRestriction) + case SCode.R_CLASS() then true; + case SCode.R_MODEL() then true; + case SCode.R_RECORD() then true; + case SCode.R_BLOCK() then true; + case SCode.R_CONNECTOR(isExpandable = _) then true; + case SCode.R_TYPE() then true; + case SCode.R_ENUMERATION() then true; + else false; + end match; +end isInstantiableClassRestriction; + end SCodeCheck; diff --git a/Compiler/FrontEnd/SCodeInst.mo b/Compiler/FrontEnd/SCodeInst.mo index 04c00faa0fa..de4d9f373ef 100644 --- a/Compiler/FrontEnd/SCodeInst.mo +++ b/Compiler/FrontEnd/SCodeInst.mo @@ -56,6 +56,7 @@ protected import Flags; protected import InstSymbolTable; protected import InstUtil; protected import List; +protected import SCodeCheck; protected import SCodeExpand; protected import SCodeFlattenRedeclare; protected import SCodeLookup; @@ -578,12 +579,15 @@ algorithm condition = NONE(), info = info), _, _, _, _) equation // Look up the class of the component. - (item, path, env) = SCodeLookup.lookupClassName(path, inEnv, info); + (item, _, env) = SCodeLookup.lookupClassName(path, inEnv, info); // Instantiate array dimensions and add them to the prefix. dims = instDimensions(ad, inEnv, inPrefix); prefix = InstUtil.addPrefix(name, dims, inPrefix); + // Check that it's legal to instantiate the class. + SCodeCheck.checkInstanceRestriction(item, prefix, info); + // Merge the class modifications with this elements' modifications. dim_count = listLength(ad); mod = SCodeMod.translateMod(smod, name, dim_count, inPrefix, inEnv); diff --git a/Compiler/Util/Error.mo b/Compiler/Util/Error.mo index f85f98d4c69..95c7424a529 100644 --- a/Compiler/Util/Error.mo +++ b/Compiler/Util/Error.mo @@ -583,6 +583,8 @@ public constant Message SIMPLIFY_CONSTANT_ERROR = MESSAGE(218, TRANSLATION(), NO "The compiler failed to perform constant folding on expression %s. Please report this bug to the developers and we will fix it as soon as possible (using the +t compiler option if possible)."); public constant Message SUM_EXPECTED_ARRAY = MESSAGE(219, TRANSLATION(), ERROR(), "In sum(%s), the expression is of type %s, but is required to be of builtin array type (of any number of dimensions)."); +public constant Message INVALID_CLASS_RESTRICTION = MESSAGE(220, TRANSLATION(), ERROR(), + "Invalid specialized class type '%s' for component %s."); public constant Message UNBOUND_PARAMETER_WARNING = MESSAGE(500, TRANSLATION(), WARNING(), "Parameter %s has neither value nor start value, and is fixed during initialization (fixed=true)");