Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit dad3995

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Use correct origin in Typing.typeCref.
- Base the origin on where the component is declared when typing a cref node, rather than where it's used. Belonging to [master]: - #2697
1 parent d59074e commit dad3995

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

Compiler/NFFrontEnd/NFCeval.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ protected
298298
Option<Expression> start_exp;
299299
Integer pcount;
300300
algorithm
301-
exp_origin := if Class.isFunction(InstNode.getClass(InstNode.parent(node)))
301+
exp_origin := if InstNode.isFunction(InstNode.explicitParent(node))
302302
then ExpOrigin.FUNCTION else ExpOrigin.CLASS;
303303

304304
Typing.typeComponentBinding(node, exp_origin);

Compiler/NFFrontEnd/NFInstNode.mo

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,16 @@ uniontype InstNode
347347
end match;
348348
end isUserdefinedClass;
349349

350+
function isFunction
351+
input InstNode node;
352+
output Boolean isFunc;
353+
algorithm
354+
isFunc := match node
355+
case CLASS_NODE() then Class.isFunction(Pointer.access(node.cls));
356+
else false;
357+
end match;
358+
end isFunction;
359+
350360
function isComponent
351361
input InstNode node;
352362
output Boolean isComponent;
@@ -503,6 +513,11 @@ uniontype InstNode
503513
end match;
504514
end parent;
505515

516+
function explicitParent
517+
input InstNode node;
518+
output InstNode parentNode = explicitScope(parent(node));
519+
end explicitParent;
520+
506521
function classParent
507522
input InstNode node;
508523
output InstNode parent;
@@ -1121,6 +1136,18 @@ uniontype InstNode
11211136
end match;
11221137
end openImplicitScope;
11231138

1139+
function explicitScope
1140+
"Returns the first parent of the node that's not an implicit scope, or the
1141+
node itself if it's not an implicit scope."
1142+
input InstNode node;
1143+
output InstNode scope;
1144+
algorithm
1145+
scope := match node
1146+
case IMPLICIT_SCOPE() then explicitScope(node.parentScope);
1147+
else node;
1148+
end match;
1149+
end explicitScope;
1150+
11241151
function addIterator
11251152
input InstNode iterator;
11261153
input output InstNode scope;

Compiler/NFFrontEnd/NFTyping.mo

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,7 @@ algorithm
12791279
Type node_ty;
12801280
list<Subscript> subs;
12811281
Variability subs_var, rest_var;
1282+
ExpOrigin.Type node_origin;
12821283

12831284
case ComponentRef.CREF(origin = Origin.SCOPE)
12841285
algorithm
@@ -1288,7 +1289,13 @@ algorithm
12881289

12891290
case ComponentRef.CREF(node = InstNode.COMPONENT_NODE())
12901291
algorithm
1291-
node_ty := typeComponent(cref.node, origin);
1292+
// The origin used when typing a component node depends on where the
1293+
// component was declared, not where it's used. This can be different to
1294+
// the given origin, e.g. for package constants used in a function.
1295+
node_origin := if InstNode.isFunction(InstNode.explicitParent(cref.node)) then
1296+
ExpOrigin.FUNCTION else ExpOrigin.CLASS;
1297+
node_ty := typeComponent(cref.node, node_origin);
1298+
12921299
(subs, subs_var) := typeSubscripts(cref.subscripts, node_ty, cref, origin, info);
12931300
(rest_cr, rest_var) := typeCref2(cref.restCref, origin, info);
12941301
subsVariability := Prefixes.variabilityMax(subs_var, rest_var);

0 commit comments

Comments
 (0)