Skip to content

Commit

Permalink
Further improve context check in typeCref (#8435)
Browse files Browse the repository at this point in the history
  • Loading branch information
perost committed Jan 18, 2022
1 parent 808cd71 commit b76366f
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
1 change: 0 additions & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -2309,7 +2309,6 @@ protected
Class cls = InstNode.getClass(node), inst_cls;
array<InstNode> local_comps, exts;
ClassTree cls_tree;
Restriction res;
array<Dimension> dims;
SourceInfo info;
Type ty;
Expand Down
10 changes: 10 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -365,6 +365,16 @@ uniontype InstNode
end match;
end isDerivedClass;

function isRootClass
input InstNode node;
output Boolean res;
algorithm
res := match node
case CLASS_NODE(nodeType = InstNodeType.ROOT_CLASS()) then true;
else false;
end match;
end isRootClass;

function isFunction
input InstNode node;
output Boolean isFunc;
Expand Down
10 changes: 10 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFRestriction.mo
Expand Up @@ -147,6 +147,16 @@ public
end match;
end isFunction;

function isRecordConstructor
input Restriction res;
output Boolean isConstructor;
algorithm
isConstructor := match res
case RECORD_CONSTRUCTOR() then true;
else false;
end match;
end isRecordConstructor;

function isRecord
input Restriction res;
output Boolean isRecord;
Expand Down
29 changes: 22 additions & 7 deletions OMCompiler/Compiler/NFFrontEnd/NFTyping.mo
Expand Up @@ -1793,9 +1793,7 @@ algorithm
Type node_ty;
list<Subscript> subs;
Variability subs_var, rest_var;
InstContext.Type node_context;
Function fn;
Restriction parent_res;

case ComponentRef.CREF(origin = Origin.SCOPE)
algorithm
Expand All @@ -1814,11 +1812,7 @@ algorithm
// The context used when typing a component node depends on where the
// component was declared, not where it's used. This can be different to
// the given context, e.g. for package constants used in a function.
parent_res := InstNode.restriction(InstNode.explicitParent(cref.node));
node_context := if Restriction.isFunction(parent_res) or
(Restriction.isRecord(parent_res) and InstContext.inFunction(context)) then
NFInstContext.FUNCTION else NFInstContext.CLASS;
node_ty := typeComponent(cref.node, node_context);
node_ty := typeComponent(cref.node, crefContext(cref.node));

(subs, subs_var) := typeSubscripts(cref.subscripts, node_ty, cref, context, info);
(rest_cr, rest_var) := typeCref2(cref.restCref, context, info, false);
Expand All @@ -1845,6 +1839,27 @@ algorithm
end match;
end typeCref2;

function crefContext
input InstNode crefNode;
output InstContext.Type context;
protected
InstNode parent;
Restriction parent_res;
algorithm
parent := InstNode.explicitParent(crefNode);

// Records might actually be record constructors that should count as
// functions here, such record constructors are always root classes.
if not InstNode.isRootClass(parent) then
context := NFInstContext.CLASS;
return;
end if;

parent_res := InstNode.restriction(parent);
context := if Restriction.isFunction(parent_res) or Restriction.isRecord(parent_res) then
NFInstContext.FUNCTION else NFInstContext.CLASS;
end crefContext;

function typeSubscripts
input list<Subscript> subscripts;
input Type crefType;
Expand Down
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -877,6 +877,7 @@ RecordBinding7.mo \
RecordBinding8.mo \
RecordBinding9.mo \
RecordBinding10.mo \
RecordBinding11.mo \
RecordConstructor1.mo \
RecordConstructor2.mo \
RecordExtends1.mo \
Expand Down
40 changes: 40 additions & 0 deletions testsuite/flattening/modelica/scodeinst/RecordBinding11.mo
@@ -0,0 +1,40 @@
// name: RecordBinding11
// keywords:
// status: correct
// cflags: -d=newInst
//

package PartialMedium
constant Real h_default = setState_pTX();

function setState_pTX
output Real state;
algorithm
state := f();
end setState_pTX;

record Coefficients
constant Real[:, 4] res = ones(3, 4);
end Coefficients;

constant Coefficients coefficients;

function f
output Real delta;
protected
final constant Integer nRes = size(coefficients.res, 1);
final constant Real[nRes, 4] b = coefficients.res;
algorithm
delta := sum(b[1, i] for i in 1:nRes);
end f;
end PartialMedium;

model RecordBinding11
parameter Real h_start = PartialMedium.h_default;
end RecordBinding11;

// Result:
// class RecordBinding11
// parameter Real h_start = 3.0;
// end RecordBinding11;
// endResult

0 comments on commit b76366f

Please sign in to comment.