Skip to content

Commit

Permalink
Improve handling of enumeration(:) (#11056)
Browse files Browse the repository at this point in the history
- Fix redeclaration of `enumeration(:)` when the redeclaring type is a
  derived type.
- Add check that there are no components with type `enumeration(:)` in
  the model.

Fixes #11053
  • Loading branch information
perost committed Aug 17, 2023
1 parent fae4e48 commit cf1ee60
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 1 deletion.
1 change: 0 additions & 1 deletion .CI/compliance.failures
Expand Up @@ -3,7 +3,6 @@ ModelicaCompliance.Algorithms.For.ShadowedIterator
ModelicaCompliance.Algorithms.For.StringRange
ModelicaCompliance.Arrays.Functions.Conversion.DimConversionMatrix
ModelicaCompliance.Classes.Declarations.Long.QuotedIdentifiers.?abfnrtv
ModelicaCompliance.Classes.Enumeration.EnumUnspecified
ModelicaCompliance.Classes.Predefined.AttributeStateSelectInvalidAlways
ModelicaCompliance.Classes.Predefined.AttributeStateSelectInvalidNever
ModelicaCompliance.Classes.Predefined.ReservedBooleanComp
Expand Down
17 changes: 17 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -710,6 +710,18 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
input output Class cls;
algorithm
() := match cls
case PARTIAL_CLASS()
algorithm
cls.prefixes := prefs;
then
();

case PARTIAL_BUILTIN()
algorithm
cls.prefixes := prefs;
then
();

case EXPANDED_CLASS()
algorithm
cls.prefixes := prefs;
Expand All @@ -722,6 +734,11 @@ constant Prefixes DEFAULT_PREFIXES = Prefixes.PREFIXES(
then
();

case INSTANCED_CLASS()
algorithm
cls.prefixes := prefs;
then
();
end match;
end setPrefixes;

Expand Down
17 changes: 17 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -617,6 +617,7 @@ protected
Prefix pre;
algorithm
Component.COMPONENT(ty = ty, binding = binding, attributes = comp_attr, comment = cmt, info = info) := comp;
checkUnspecifiedEnumType(ty, node, info);
var := comp_attr.variability;

if isSome(outerBinding) then
Expand Down Expand Up @@ -667,6 +668,22 @@ algorithm
vars := Variable.VARIABLE(name, ty, binding, visibility, comp_attr, ty_attrs, children, cmt, info, NFBackendExtension.DUMMY_BACKEND_INFO) :: vars;
end flattenSimpleComponent;

function checkUnspecifiedEnumType
input Type ty;
input InstNode node;
input SourceInfo info;
algorithm
() := match ty
case Type.ENUMERATION(literals = {})
algorithm
Error.addSourceMessage(Error.UNSPECIFIED_ENUM_COMPONENT, {InstNode.name(node)}, info);
then
fail();

else ();
end match;
end checkUnspecifiedEnumType;

function flattenTypeAttribute
input Modifier attr;
input Prefix prefix;
Expand Down
14 changes: 14 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -1664,10 +1664,24 @@ function redeclareEnum
input InstNode originalNode;
output Class redeclaredClass = redeclareClass;
algorithm
// Expand the redeclare node so we can check whether it's an enumeration or not.
expand(redeclareNode);
redeclaredClass := InstNode.getClass(redeclareNode);

redeclaredClass := match (redeclaredClass, originalClass)
local
list<String> lits1, lits2;

// Redeclare enumeration(:).
case (_, Class.PARTIAL_BUILTIN(ty = Type.ENUMERATION(literals = {})))
guard InstNode.isEnumerationType(redeclareNode)
algorithm
redeclaredClass := Class.setPrefixes(prefixes, redeclaredClass);
redeclaredClass := Class.mergeModifier(outerMod, redeclaredClass);
then
redeclaredClass;

// Redeclare normal enumeration.
case (Class.PARTIAL_BUILTIN(ty = Type.ENUMERATION(literals = lits1)),
Class.PARTIAL_BUILTIN(ty = Type.ENUMERATION(literals = lits2)))
algorithm
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/Util/Error.mo
Expand Up @@ -887,6 +887,8 @@ public constant ErrorTypes.Message PARTIAL_DERIVATIVE_INPUT_INVALID_TYPE = Error
Gettext.gettext("'%s' in partial derivative of '%s' is not a scalar Real input parameter of the function."));
public constant ErrorTypes.Message CONNECT_TYPE_MISMATCH = ErrorTypes.MESSAGE(406, ErrorTypes.TRANSLATION(), ErrorTypes.ERROR(),
Gettext.gettext("The connectors in connect(%s, %s) are not type compatible."));
public constant ErrorTypes.Message UNSPECIFIED_ENUM_COMPONENT = ErrorTypes.MESSAGE(407, ErrorTypes.TRANSLATION(), ErrorTypes.ERROR(),
Gettext.gettext("Component '%s' has an unspecified enumeration type (enumeration(:))."));

public constant ErrorTypes.Message INITIALIZATION_NOT_FULLY_SPECIFIED = ErrorTypes.MESSAGE(496, ErrorTypes.TRANSLATION(), ErrorTypes.WARNING(),
Gettext.gettext("The initial conditions are not fully specified. %s."));
Expand Down
20 changes: 20 additions & 0 deletions testsuite/flattening/modelica/scodeinst/EnumUnspecified1.mo
@@ -0,0 +1,20 @@
// name: EnumUnspecified1
// keywords:
// status: incorrect
// cflags: -d=newInst
//

model EnumUnspecified1
replaceable type E = enumeration(:);
E e;
end EnumUnspecified1;

// Result:
// Error processing file: EnumUnspecified1.mo
// [flattening/modelica/scodeinst/EnumUnspecified1.mo:9:3-9:6:writable] Error: Component 'e' has an unspecified enumeration type (enumeration(:)).
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
//
// Execution failed!
// endResult
3 changes: 3 additions & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -407,6 +407,7 @@ EnumConversion1.mo \
EnumConversion2.mo \
EnumInvalidLiteralName1.mo \
EnumRangeBinding1.mo \
EnumUnspecified1.mo \
eq1.mo \
eq2.mo \
eq3.mo \
Expand Down Expand Up @@ -996,6 +997,8 @@ RedeclareEnum1.mo \
RedeclareEnum2.mo \
RedeclareEnum3.mo \
RedeclareEnum4.mo \
RedeclareEnum5.mo \
RedeclareEnum6.mo \
RedeclareFunction1.mo \
RedeclareInvalidConnectorType1.mo \
RedeclareInvalidVariability1.mo \
Expand Down
23 changes: 23 additions & 0 deletions testsuite/flattening/modelica/scodeinst/RedeclareEnum5.mo
@@ -0,0 +1,23 @@
// name: RedeclareEnum5
// keywords:
// status: correct
// cflags: -d=newInst
//

model A
replaceable type E = enumeration(:);
E e;
end A;

model RedeclareEnum5
extends A(redeclare type E = E3);
type E2 = enumeration(a, b, c);
type E3 = E2;
end RedeclareEnum5;


// Result:
// class RedeclareEnum5
// enumeration(a, b, c) e;
// end RedeclareEnum5;
// endResult
27 changes: 27 additions & 0 deletions testsuite/flattening/modelica/scodeinst/RedeclareEnum6.mo
@@ -0,0 +1,27 @@
// name: RedeclareEnum6
// keywords:
// status: incorrect
// cflags: -d=newInst
//

model A
replaceable type E = enumeration(:);
E e;
end A;

model RedeclareEnum6
extends A(redeclare type E = E2);
type E2 = Real;
end RedeclareEnum6;


// Result:
// Error processing file: RedeclareEnum6.mo
// [flattening/modelica/scodeinst/RedeclareEnum6.mo:13:23-13:34:writable] Notification: From here:
// [flattening/modelica/scodeinst/RedeclareEnum6.mo:8:15-8:38:writable] Error: Redeclaration of enumeration 'E' is not a subtype of the redeclared element.
//
// # Error encountered! Exiting...
// # Please check the error message and the flags.
//
// Execution failed!
// endResult

0 comments on commit cf1ee60

Please sign in to comment.