Skip to content

Commit

Permalink
- Better error messages for lookup errors in new instantiation.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@18446 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Dec 9, 2013
1 parent 2781d79 commit 7341889
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 30 deletions.
16 changes: 16 additions & 0 deletions Compiler/FrontEnd/Absyn.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2948,6 +2948,22 @@ algorithm
end match;
end pathFirstIdent;

public function pathSecondIdent
input Path inPath;
output Ident outIdent;
algorithm
outIdent := match(inPath)
local
Ident n;
Path p;

case QUALIFIED(path = QUALIFIED(name = n)) then n;
case QUALIFIED(path = IDENT(name = n)) then n;
case FULLYQUALIFIED(path = p) then pathSecondIdent(p);

end match;
end pathSecondIdent;

public function pathPrefix
"Returns the prefix of a path, i.e. this.is.a.path => this.is.a"
input Path path;
Expand Down
5 changes: 5 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,11 @@ algorithm
case (DAE.DIV_SCALAR_ARRAY(ty = _), _) then DAE.DIV(inType);
case (DAE.POW_ARRAY_SCALAR(ty = _), _) then DAE.POW(inType);
case (DAE.POW_SCALAR_ARRAY(ty = _), _) then DAE.POW(inType);
case (DAE.UMINUS_ARR(ty = _), _) then DAE.UMINUS(inType);
case (DAE.ADD_ARR(ty = _), _) then DAE.ADD(inType);
case (DAE.SUB_ARR(ty = _), _) then DAE.SUB(inType);
case (DAE.MUL_ARR(ty = _), _) then DAE.MUL(inType);
case (DAE.DIV_ARR(ty = _), _) then DAE.DIV(inType);
else inOperator;
end match;
end makeScalarOpFromArrayOp;
Expand Down
105 changes: 76 additions & 29 deletions Compiler/FrontEnd/NFLookup.mo
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ protected uniontype LookupState
record STATE_FUNC "Found name is function." end STATE_FUNC;
record STATE_PREDEF_COMP "Found name is predefined component." end STATE_PREDEF_COMP;
record STATE_PREDEF_CLASS "Found name is predefined class." end STATE_PREDEF_CLASS;
record STATE_ERROR "An error occured during lookup."
LookupState errorState;
end STATE_ERROR;
end LookupState;

public function lookupClassName
Expand All @@ -96,7 +99,7 @@ protected
algorithm
(outEntry, outEnv, state) := lookupName(inName, inEnv, STATE_BEGIN(), inInfo,
Error.LOOKUP_ERROR);
validateEndState(state, STATE_CLASS(), inName, inInfo);
validateEndState(state, STATE_CLASS(), outEntry, inName, inInfo);
end lookupClassName;

public function lookupBaseClassName
Expand All @@ -111,7 +114,7 @@ protected
algorithm
(outEntry, outEnv, state) := lookupName(inName, inEnv, STATE_BEGIN(), inInfo,
Error.LOOKUP_BASECLASS_ERROR);
validateEndState(state, STATE_CLASS(), inName, inInfo);
validateEndState(state, STATE_CLASS(), outEntry, inName, inInfo);
end lookupBaseClassName;

public function lookupVariableName
Expand All @@ -127,7 +130,7 @@ algorithm
(outEntry, outEnv, state) := lookupName(inName, inEnv, STATE_BEGIN(), inInfo,
Error.LOOKUP_VARIABLE_ERROR);
state := fixEnumTypenameLookup(state, outEntry);
validateEndState(state, STATE_COMP(), inName, inInfo);
validateEndState(state, STATE_COMP(), outEntry, inName, inInfo);
end lookupVariableName;

protected function fixEnumTypenameLookup
Expand Down Expand Up @@ -160,7 +163,7 @@ algorithm
/* TODO: Handle Integer and String also. */
(outEntry, outEnv, state) := lookupName(inName, inEnv, STATE_BEGIN(), inInfo,
Error.LOOKUP_FUNCTION_ERROR);
validateEndState(state, STATE_FUNC(), inName, inInfo);
validateEndState(state, STATE_FUNC(), outEntry, inName, inInfo);
end lookupFunctionName;

public function lookupLocalName
Expand Down Expand Up @@ -384,32 +387,34 @@ protected function validateEndState
STATE_FUNC, and represents the kind of element we expected to find."
input LookupState inEndState;
input LookupState inExpectedState;
input Entry inEntry;
input Absyn.Path inName;
input Absyn.Info inInfo;
algorithm
_ := match(inEndState, inExpectedState, inName, inInfo)
_ := match(inEndState, inExpectedState, inEntry, inName, inInfo)
local
String name, found_str, expected_str;
String name, name2, full_name, found_str, expected_str;
Absyn.Info info;

// Found the expected kind of element.
case (STATE_COMP(), STATE_COMP(), _, _) then ();
case (STATE_COMP_COMP(), STATE_COMP(), _, _) then ();
case (STATE_PREDEF_COMP(), STATE_COMP(), _, _) then ();
case (STATE_PACKAGE(), STATE_CLASS(), _, _) then ();
case (STATE_CLASS(), STATE_CLASS(), _, _) then ();
case (STATE_PREDEF_CLASS(), STATE_CLASS(), _, _) then ();
case (STATE_FUNC(), STATE_FUNC(), _, _) then ();
case (STATE_COMP_FUNC(), STATE_FUNC(), _, _) then ();
case (STATE_COMP(), STATE_COMP(), _, _, _) then ();
case (STATE_COMP_COMP(), STATE_COMP(), _, _, _) then ();
case (STATE_PREDEF_COMP(), STATE_COMP(), _, _, _) then ();
case (STATE_PACKAGE(), STATE_CLASS(), _, _, _) then ();
case (STATE_CLASS(), STATE_CLASS(), _, _, _) then ();
case (STATE_PREDEF_CLASS(), STATE_CLASS(), _, _, _) then ();
case (STATE_FUNC(), STATE_FUNC(), _, _, _) then ();
case (STATE_COMP_FUNC(), STATE_FUNC(), _, _, _) then ();

// Found a class via a component, but expected a function.
case (STATE_COMP_CLASS(), STATE_FUNC(), _, _)
case (STATE_COMP_CLASS(), STATE_FUNC(), _, _, _)
equation
printFoundWrongTypeError(inEndState, inExpectedState, inName, inInfo);
then
fail();

// Found a function via a component, but didn't expect a function.
case (STATE_COMP_FUNC(), _, _, _)
case (STATE_COMP_FUNC(), _, _, _, _)
equation
name = Absyn.pathString(inName);
Error.addSourceMessage(Error.FOUND_FUNC_NAME_VIA_COMP_NONCALL, {name}, inInfo);
Expand All @@ -418,13 +423,51 @@ algorithm

// Found a class via a component. Only component and functions are allowed
// to be looked up via a component.
case (STATE_COMP_CLASS(), _, _, _)
case (STATE_COMP_CLASS(), _, _, _, _)
equation
name = Absyn.pathString(inName);
Error.addSourceMessage(Error.FOUND_CLASS_NAME_VIA_COMPONENT, {name}, inInfo);
then
fail();

// Invalid form when looking for a function via a component, only c.C1..Cn.f
// is allowed.
case (STATE_ERROR(errorState = STATE_COMP_FUNC()), STATE_FUNC(), _, _, _)
equation
(name, info) = SCode.elementNameInfo(NFEnv.entryElement(inEntry));
Error.addSourceMessage(Error.NON_CLASS_IN_COMP_FUNC_NAME, {name}, info);
then
fail();

// Invalid lookup of non-function via component.
case (STATE_ERROR(errorState = STATE_COMP_FUNC()), _, _, _, _)
equation
name = Absyn.pathFirstIdent(inName);
name2 = Absyn.pathSecondIdent(inName);
full_name = Absyn.pathString(inName);
Error.addSourceMessage(Error.LOOKUP_VIA_COMP_NON_FUNCALL,
{name2, name, full_name}, inInfo);
then
fail();

// Found class when looking up a composite component name.
case (STATE_ERROR(errorState = STATE_COMP_COMP()), STATE_COMP(), _, _, _)
equation
(name, info) = SCode.elementNameInfo(NFEnv.entryElement(inEntry));
Error.addSourceMessage(Error.CLASS_IN_COMPOSITE_COMP_NAME, {name}, info);
then
fail();

// Found class via composite component name when actually looking for a class.
case (STATE_ERROR(errorState = STATE_COMP_COMP()), _, _, _, _)
equation
name = SCode.elementName(NFEnv.entryElement(inEntry));
full_name = Absyn.pathString(inName);
Error.addSourceMessage(Error.LOOKUP_CLASS_VIA_COMP_COMP,
{name, full_name}, inInfo);
then
fail();

// Found the wrong kind of element.
else
equation
Expand Down Expand Up @@ -573,8 +616,7 @@ protected function nextState2
algorithm
outNextState := match(inElementState, inCurrentState, inElement)
local
String name;
Absyn.Info info;
String str;

// Transitions from BEGIN.
case (_, STATE_BEGIN(), _) then inElementState;
Expand Down Expand Up @@ -610,23 +652,23 @@ algorithm
case (STATE_PACKAGE(), STATE_COMP_FUNC(), _) then STATE_COMP_CLASS();

// When looking for a function in a component the only valid form is
// c.M1..MN.f, where M1..Mn are classes, but we found a component instead.
// c.M1..Mn.f, where M1..Mn are classes, but we found a component instead.
case (STATE_COMP(), _, _)
equation
(name, info) = SCode.elementNameInfo(inElement);
Error.addSourceMessage(Error.NON_CLASS_IN_COMP_FUNC_NAME, {name}, info);
then
fail();

then STATE_ERROR(STATE_COMP_FUNC());

// We found a class when only components are allowed, i.e. when not looking
// for a function via a component.
case (_, STATE_COMP_COMP(), _)
then STATE_ERROR(STATE_COMP_COMP());

else
equation
(name, info) = SCode.elementNameInfo(inElement);
Error.addSourceMessage(Error.CLASS_IN_COMPOSITE_COMP_NAME, {name}, info);
str = SCode.elementName(inElement);
str = "NFLookup.nextState2 failed on unknown transition for element " +& str;
Error.addMessage(Error.INTERNAL_ERROR, {str});
then
fail();

end match;
end nextState2;

Expand Down Expand Up @@ -831,6 +873,11 @@ algorithm
Env env;
LookupState state;

// An error has occured, return the entry found so far and let the caller
// handle the error reporting.
case (_, _, _, STATE_ERROR(_))
then (inEntry, inEnv, inState);

case (_, _, _, _)
equation
(env, _) = enterEntryScope(inEntry, NFInstTypes.NOMOD(), NONE(), inEnv);
Expand Down
1 change: 1 addition & 0 deletions Compiler/FrontEnd/NFSCodeExpand.mo
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ algorithm
equation
e1 = subscriptExp(e1, inEqSubscripts, inAllSubscripts);
e2 = subscriptExp(e2, inEqSubscripts, inAllSubscripts);
op = Expression.unliftOperatorX(op, listLength(inEqSubscripts));
then
DAE.BINARY(e1, op, e2);

Expand Down
6 changes: 5 additions & 1 deletion Compiler/Util/Error.mo
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ public constant Message ARRAY_INDEX_OUT_OF_BOUNDS = MESSAGE(99, TRANSLATION(), E
public constant Message COMPONENT_CONDITION_VARIABILITY = MESSAGE(100, TRANSLATION(), ERROR(),
Util.gettext("Component condition must be parameter or constant expression (in %s)."));
public constant Message FOUND_CLASS_NAME_VIA_COMPONENT = MESSAGE(101, TRANSLATION(), ERROR(),
Util.gettext("Found class %s by name lookup via component. Only component and function call names may be looked up via a component."));
Util.gettext("Found class %s by name lookup via component (only component and function call names may be looked up via a component)."));
public constant Message FOUND_FUNC_NAME_VIA_COMP_NONCALL = MESSAGE(102, TRANSLATION(), ERROR(),
Util.gettext("Found function %s by name lookup via component, but this is only valid when the name is used as a function call."));
public constant Message DUPLICATE_MODIFICATIONS = MESSAGE(103, TRANSLATION(), ERROR(),
Expand All @@ -345,6 +345,10 @@ public constant Message NON_PARAMETER_ITERATOR_RANGE = MESSAGE(109, TRANSLATION(
Util.gettext("The iteration range %s is not a constant or parameter expression."));
public constant Message IMPLICIT_ITERATOR_NOT_FOUND_IN_LOOP_BODY = MESSAGE(110, TRANSLATION(), ERROR(),
Util.gettext("Identifier %s of implicit for iterator must be present as array subscript in the loop body."));
public constant Message LOOKUP_VIA_COMP_NON_FUNCALL = MESSAGE(111, TRANSLATION(), ERROR(),
Util.gettext("Lookup of element %s is not allowed via component %s when looking for %s (only function calls may be looked up via a component)."));
public constant Message LOOKUP_CLASS_VIA_COMP_COMP = MESSAGE(112, TRANSLATION(), ERROR(),
Util.gettext("Lookup of class %s is not allowed in composite component name %s (only components may be looked up in this way)."));
public constant Message IF_EQUATION_UNBALANCED = MESSAGE(114, TRANSLATION(), ERROR(),
Util.gettext("In equation %s. If-equation with conditions that are not parameter expressions must have the same number of equations in each branch, equation count is %s for each respective branch."));
public constant Message IF_EQUATION_MISSING_ELSE = MESSAGE(115, TRANSLATION(), ERROR(),
Expand Down

0 comments on commit 7341889

Please sign in to comment.