Skip to content

Commit

Permalink
- Cleaned up SCode.makeEnumType.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@8769 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed May 2, 2011
1 parent 2d410db commit b158776
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 32 deletions.
47 changes: 16 additions & 31 deletions Compiler/FrontEnd/SCode.mo
Expand Up @@ -566,12 +566,16 @@ public constant Prefixes defaultPrefixes =
Absyn.NOT_INNER_OUTER(),
NOT_REPLACEABLE());

public constant Attributes defaultConstAttr =
ATTR({}, NOT_FLOW(), NOT_STREAM(), RO(), CONST(), Absyn.BIDIR());

// .......... functionality .........
protected import Util;
protected import Dump;
protected import ModUtil;
protected import Print;
protected import Error;
protected import SCodeCheck;

protected function elseWhenEquationStr
"@author: adrpo
Expand Down Expand Up @@ -2619,39 +2623,20 @@ end printInitialStr;
public function makeEnumType
"Creates an EnumType element from an enumeration literal and an optional
comment."
input Enum enum;
input Absyn.Info info;
output Element enum_type;
algorithm
enum_type := matchcontinue(enum, info)
local
String literal,info_str;
Option<Comment> comment;
case (ENUM(literal = literal, comment = comment), _)
equation
isValidEnumLiteral(literal);
then
COMPONENT(
literal,
PREFIXES(PUBLIC(), NOT_REDECLARE(), FINAL(), Absyn.NOT_INNER_OUTER(), NOT_REPLACEABLE()),
ATTR({}, NOT_FLOW(), NOT_STREAM(), RO(), CONST(), Absyn.BIDIR()),
Absyn.TPATH(Absyn.IDENT("EnumType"),NONE()),
NOMOD(), comment, NONE(), info);
case (ENUM(literal = literal), _)
equation
info_str = Error.infoStr(info);
Error.addMessage(Error.INVALID_ENUM_LITERAL, {info_str, literal});
then fail();
end matchcontinue;
input Enum inEnum;
input Absyn.Info inInfo;
output Element outEnumType;
protected
String literal;
Option<Comment> comment;
algorithm
ENUM(literal = literal, comment = comment) := inEnum;
SCodeCheck.checkValidEnumLiteral(literal, inInfo);
outEnumType := COMPONENT(literal, defaultPrefixes, defaultConstAttr,
Absyn.TPATH(Absyn.IDENT("EnumType"), NONE()),
NOMOD(), comment, NONE(), inInfo);
end makeEnumType;

public function isValidEnumLiteral
"Checks if a string is a valid enumeration literal."
input String literal;
algorithm
true := Util.listNotContains(literal, {"quantity", "min", "max", "start", "fixed"});
end isValidEnumLiteral;

public function variabilityOr
"returns the more constant of two Variabilities (considers VAR() < DISCRETE() < PARAM() < CONST() ), similarly to Types.constOr"
input Variability inConst1;
Expand Down
20 changes: 20 additions & 0 deletions Compiler/FrontEnd/SCodeCheck.mo
Expand Up @@ -268,4 +268,24 @@ algorithm
end matchcontinue;
end checkRedeclareModifier2;

public function checkValidEnumLiteral
input String inLiteral;
input Absyn.Info inInfo;
algorithm
_ := matchcontinue(inLiteral, inInfo)
case (_, _)
equation
true = Util.listNotContains(inLiteral,
{"quantity", "min", "max", "start", "fixed"});
then
();

else
equation
Error.addSourceMessage(Error.INVALID_ENUM_LITERAL, {inLiteral}, inInfo);
then
fail();
end matchcontinue;
end checkValidEnumLiteral;

end SCodeCheck;
2 changes: 1 addition & 1 deletion Compiler/Util/Error.mo
Expand Up @@ -464,7 +464,7 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
(NESTED_WHEN, TRANSLATION(), ERROR(),
"Nested when statements are not allowed."),
(INVALID_ENUM_LITERAL, TRANSLATION(), ERROR(),
"%s Invalid use of reserved attribute name %s as enumeration literal."),
"Invalid use of reserved attribute name %s as enumeration literal."),
(UNEXCPECTED_FUNCTION_INPUTS_WARNING,TRANSLATION(), WARNING(),
"Function %s has not the expected inputs. Expected inputs are %s."),
(RESTRICTION_VIOLATION,TRANSLATION(),ERROR(),"Restriction violation: %s is a %s, not a %s"),
Expand Down

0 comments on commit b158776

Please sign in to comment.