Skip to content

Commit

Permalink
[NF] Propagate comments to the DAE.
Browse files Browse the repository at this point in the history
- Add comments to Component and use ElementSource for
  equations/algorithms instead of SourceInfo.
- Generate comments for generated connect equations.
- Fix instantiation of if-statements so it doesn't add an else branch
  when there is none.

Belonging to [master]:
  - OpenModelica/OMCompiler#2122
  - OpenModelica/OpenModelica-testsuite#831
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jan 25, 2018
1 parent c28b630 commit eb49dd7
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 277 deletions.
2 changes: 2 additions & 0 deletions Compiler/FrontEnd/ElementSource.mo
Expand Up @@ -412,6 +412,8 @@ algorithm
end match;
end getStatementSource;

function getInfo = getElementSourceFileInfo;

function getElementSourceFileInfo
"Gets the file information associated with an element.
If there are several candidates, select the first one."
Expand Down
6 changes: 6 additions & 0 deletions Compiler/NFFrontEnd/NFBuiltin.mo
Expand Up @@ -236,6 +236,7 @@ constant InstNode STATESELECT_NEVER =
STATESELECT_NEVER_BINDING,
Binding.UNBOUND(),
NFComponent.CONSTANT_ATTR,
NONE(),
Absyn.dummyInfo)),
0,
STATESELECT_NODE);
Expand All @@ -252,6 +253,7 @@ constant InstNode STATESELECT_AVOID =
STATESELECT_AVOID_BINDING,
Binding.UNBOUND(),
NFComponent.CONSTANT_ATTR,
NONE(),
Absyn.dummyInfo)),
0,
STATESELECT_NODE);
Expand All @@ -268,6 +270,7 @@ constant InstNode STATESELECT_DEFAULT =
STATESELECT_DEFAULT_BINDING,
Binding.UNBOUND(),
NFComponent.CONSTANT_ATTR,
NONE(),
Absyn.dummyInfo)),
0,
STATESELECT_NODE);
Expand All @@ -284,6 +287,7 @@ constant InstNode STATESELECT_PREFER =
STATESELECT_PREFER_BINDING,
Binding.UNBOUND(),
NFComponent.CONSTANT_ATTR,
NONE(),
Absyn.dummyInfo)),
0,
STATESELECT_NODE);
Expand All @@ -300,6 +304,7 @@ constant InstNode STATESELECT_ALWAYS =
STATESELECT_ALWAYS_BINDING,
Binding.UNBOUND(),
NFComponent.CONSTANT_ATTR,
NONE(),
Absyn.dummyInfo)),
0,
STATESELECT_NODE);
Expand Down Expand Up @@ -332,6 +337,7 @@ constant InstNode TIME =
Binding.UNBOUND(),
Binding.UNBOUND(),
NFComponent.INPUT_ATTR,
NONE(),
Absyn.dummyInfo)),
0,
InstNode.EMPTY_NODE());
Expand Down
10 changes: 5 additions & 5 deletions Compiler/NFFrontEnd/NFBuiltinFuncs.mo
Expand Up @@ -60,39 +60,39 @@ constant SCode.Element DUMMY_ELEMENT = SCode.COMPONENT("dummy",

// Default Integer parameter.
constant Component INT_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.INTEGER(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, Absyn.dummyInfo);
Type.INTEGER(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode INT_PARAM = InstNode.COMPONENT_NODE("i",
Visibility.PUBLIC,
Pointer.createImmutable(INT_COMPONENT), 0, InstNode.EMPTY_NODE());

// Default Real parameter.
constant Component REAL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.REAL(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, Absyn.dummyInfo);
Type.REAL(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode REAL_PARAM = InstNode.COMPONENT_NODE("r",
Visibility.PUBLIC,
Pointer.createImmutable(REAL_COMPONENT), 0, InstNode.EMPTY_NODE());

// Default Boolean parameter.
constant Component BOOL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.BOOLEAN(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, Absyn.dummyInfo);
Type.BOOLEAN(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode BOOL_PARAM = InstNode.COMPONENT_NODE("b",
Visibility.PUBLIC,
Pointer.createImmutable(BOOL_COMPONENT), 0, InstNode.EMPTY_NODE());

// Default String parameter.
constant Component STRING_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.STRING(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, Absyn.dummyInfo);
Type.STRING(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode STRING_PARAM = InstNode.COMPONENT_NODE("s",
Visibility.PUBLIC,
Pointer.createImmutable(STRING_COMPONENT), 0, InstNode.EMPTY_NODE());

// Default enumeration(:) parameter.
constant Component ENUM_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.ENUMERATION_ANY(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, Absyn.dummyInfo);
Type.ENUMERATION_ANY(), Binding.UNBOUND(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR, NONE(), Absyn.dummyInfo);

constant InstNode ENUM_PARAM = InstNode.COMPONENT_NODE("e",
Visibility.PUBLIC,
Expand Down
15 changes: 14 additions & 1 deletion Compiler/NFFrontEnd/NFComponent.mo
Expand Up @@ -116,6 +116,7 @@ uniontype Component
Binding binding;
Binding condition;
Component.Attributes attributes;
Option<SCode.Comment> comment;
SourceInfo info;
end UNTYPED_COMPONENT;

Expand All @@ -125,6 +126,7 @@ uniontype Component
Binding binding;
Binding condition;
Component.Attributes attributes;
Option<SCode.Comment> comment;
SourceInfo info;
end TYPED_COMPONENT;

Expand Down Expand Up @@ -258,7 +260,7 @@ uniontype Component
component := match component
case UNTYPED_COMPONENT()
then TYPED_COMPONENT(component.classInst, ty, component.binding,
component.condition, component.attributes, component.info);
component.condition, component.attributes, component.comment, component.info);

case TYPED_COMPONENT()
algorithm
Expand Down Expand Up @@ -558,6 +560,17 @@ uniontype Component
end match;
end dimensionCount;

function comment
input Component component;
output Option<SCode.Comment> comment;
algorithm
comment := match component
case COMPONENT_DEF() then SCode.getElementComment(component.definition);
case UNTYPED_COMPONENT() then component.comment;
case TYPED_COMPONENT() then component.comment;
end match;
end comment;

end Component;

annotation(__OpenModelica_Interface="frontend");
Expand Down
20 changes: 9 additions & 11 deletions Compiler/NFFrontEnd/NFConnectEquations.mo
Expand Up @@ -191,11 +191,11 @@ function makeEqualityAssert
input DAE.ElementSource rhsSource;
output Equation equalityAssert;
protected
//DAE.ElementSource source;
DAE.ElementSource source;
Expression lhs_exp, rhs_exp, exp;
Type ty;
algorithm
//source := ElementSource.mergeSources(lhsSource, rhsSource);
source := ElementSource.mergeSources(lhsSource, rhsSource);
//source := ElementSource.addElementSourceConnect(source, (lhsCref, rhsCref));

ty := ComponentRef.getType(lhsCref);
Expand All @@ -213,7 +213,7 @@ algorithm
exp := Expression.RELATION(lhs_exp, Operator.EQUAL(ty), rhs_exp);
end if;

equalityAssert := Equation.ASSERT(exp, EQ_ASSERT_STR, NFBuiltin.ASSERTIONLEVEL_ERROR, Absyn.dummyInfo);
equalityAssert := Equation.ASSERT(exp, EQ_ASSERT_STR, NFBuiltin.ASSERTIONLEVEL_ERROR, source);
end makeEqualityAssert;

//protected function shouldFlipPotentialEquation
Expand Down Expand Up @@ -260,8 +260,7 @@ algorithm
end for;
end if;

// TODO: Change equations to use ElementSource instead of SourceInfo.
equations := {Equation.EQUALITY(sum, Expression.REAL(0.0), Type.REAL(), Absyn.dummyInfo)};
equations := {Equation.EQUALITY(sum, Expression.REAL(0.0), Type.REAL(), src)};
end generateFlowEquations;

function makeFlowExp
Expand Down Expand Up @@ -311,10 +310,10 @@ algorithm
cref2 := Expression.fromCref(cr2);
e1 := makeInStreamCall(cref2);
e2 := makeInStreamCall(cref1);
//src := ElementSource.mergeSources(src1, src2);
src := ElementSource.mergeSources(src1, src2);
then
{Equation.EQUALITY(cref1, e1, Type.REAL(), Absyn.dummyInfo),
Equation.EQUALITY(cref2, e2, Type.REAL(), Absyn.dummyInfo)};
{Equation.EQUALITY(cref1, e1, Type.REAL(), src),
Equation.EQUALITY(cref2, e2, Type.REAL(), src)};

// One inside, one outside:
// cr1 = cr2;
Expand Down Expand Up @@ -348,11 +347,10 @@ protected
algorithm
for e in outsideElements loop
cref_exp := Expression.fromCref(e.name);
//outside := listRest(outside);
outside := removeStreamSetElement(e.name, outsideElements);
res := streamSumEquationExp(outside, insideElements, flowThreshold);
//src := ElementSource.addAdditionalComment(e.source, " equation generated from stream connection");
equations := Equation.EQUALITY(cref_exp, res, Type.REAL(), Absyn.dummyInfo) :: equations;
src := ElementSource.addAdditionalComment(e.source, " equation generated from stream connection");
equations := Equation.EQUALITY(cref_exp, res, Type.REAL(), src) :: equations;
end for;
end streamEquationGeneral;

Expand Down
11 changes: 6 additions & 5 deletions Compiler/NFFrontEnd/NFConnections.mo
Expand Up @@ -43,6 +43,7 @@ protected
import Expression = NFExpression;
import Type = NFType;
import MetaModelica.Dangerous.listReverseInPlace;
import ElementSource;

public
record CONNECTIONS
Expand Down Expand Up @@ -76,7 +77,7 @@ public
ComponentRef cr, lhs, rhs;
Connector c1, c2;
Type ty1, ty2;
SourceInfo info;
DAE.ElementSource source;
list<Equation> eql = {};
algorithm
// Collect all flow variables.
Expand All @@ -85,7 +86,7 @@ public

if Component.isFlow(comp) then
c1 := Connector.fromFacedCref(var.name, var.ty,
NFConnector.Face.INSIDE, Component.info(comp));
NFConnector.Face.INSIDE, ElementSource.createElementSource(Component.info(comp)));
conns := addFlow(c1, conns);
end if;
end for;
Expand All @@ -94,10 +95,10 @@ public
for eq in flatModel.equations loop
eql := match eq
case Equation.CONNECT(lhs = Expression.CREF(cref = lhs, ty = ty1),
rhs = Expression.CREF(cref = rhs, ty = ty2), info = info)
rhs = Expression.CREF(cref = rhs, ty = ty2), source = source)
algorithm
c1 := Connector.fromCref(lhs, ty1, info);
c2 := Connector.fromCref(rhs, ty2, info);
c1 := Connector.fromCref(lhs, ty1, source);
c2 := Connector.fromCref(rhs, ty2, source);
conns := addConnection(Connection.CONNECTION(c1, c2), conns);
then
eql;
Expand Down
9 changes: 4 additions & 5 deletions Compiler/NFFrontEnd/NFConnector.mo
Expand Up @@ -64,22 +64,21 @@ public
function fromCref
input ComponentRef cref;
input Type ty;
input SourceInfo info;
output Connector conn = fromFacedCref(cref, ty, crefFace(cref), info);
input DAE.ElementSource source;
output Connector conn = fromFacedCref(cref, ty, crefFace(cref), source);
end fromCref;

function fromFacedCref
input ComponentRef cref;
input Type ty;
input Face face;
input SourceInfo info;
input DAE.ElementSource source;
output Connector conn;
protected
InstNode node = ComponentRef.node(cref);
algorithm
conn := CONNECTOR(ComponentRef.simplifySubscripts(cref), ty, face,
Component.connectorType(InstNode.component(node)), NONE(),
ElementSource.createElementSource(InstNode.info(node)));
Component.connectorType(InstNode.component(node)), NONE(), source);
end fromFacedCref;

function getType
Expand Down

0 comments on commit eb49dd7

Please sign in to comment.