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

Commit 669113d

Browse files
perostOpenModelica-Hudson
authored andcommitted
Fix for #3535.
- Detect duplicate classes and give an error.
1 parent eae1de1 commit 669113d

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

Compiler/FFrontEnd/FGraph.mo

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,7 @@ public function mkClassNode
10851085
input SCode.Element inClass;
10861086
input Prefix.Prefix inPrefix;
10871087
input DAE.Mod inMod;
1088+
input Boolean checkDuplicate = false;
10881089
output Graph outGraph;
10891090
algorithm
10901091
outGraph := matchcontinue (inGraph, inClass, inPrefix, inMod)
@@ -1105,7 +1106,8 @@ algorithm
11051106
case (g, SCode.CLASS(), _, _)
11061107
equation
11071108
r = lastScopeRef(g);
1108-
g = FGraphBuildEnv.mkClassNode(inClass, inPrefix, inMod, r, FCore.USERDEFINED(), g);
1109+
g = FGraphBuildEnv.mkClassNode(inClass, inPrefix, inMod, r,
1110+
FCore.USERDEFINED(), g, checkDuplicate);
11091111
then
11101112
g;
11111113

Compiler/FFrontEnd/FGraphBuildEnv.mo

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ public function mkProgramGraph
7878
"builds nodes out of classes"
7979
input SCode.Program inProgram;
8080
input Kind inKind;
81-
input Graph inGraph;
82-
output Graph outGraph;
81+
input output Graph graph;
8382
protected
8483
Ref topRef;
8584
algorithm
86-
topRef := FGraph.top(inGraph);
87-
outGraph := List.fold2(inProgram, mkClassGraph, topRef, inKind, inGraph);
85+
topRef := FGraph.top(graph);
86+
87+
for cls in inProgram loop
88+
graph := mkClassGraph(cls, topRef, inKind, graph, true);
89+
end for;
8890
end mkProgramGraph;
8991

9092
protected function mkClassGraph
@@ -93,6 +95,7 @@ protected function mkClassGraph
9395
input Ref inParentRef;
9496
input Kind inKind;
9597
input Graph inGraph;
98+
input Boolean checkDuplicate = false;
9699
output Graph outGraph;
97100
algorithm
98101
outGraph := match(inClass, inParentRef, inKind, inGraph)
@@ -105,7 +108,8 @@ algorithm
105108
// class (we don't care here if is replaceable or not we can get that from the class)
106109
case (SCode.CLASS(), _, _, g)
107110
equation
108-
g = mkClassNode(inClass, Prefix.NOPRE(), DAE.NOMOD(), inParentRef, inKind, g);
111+
g = mkClassNode(inClass, Prefix.NOPRE(), DAE.NOMOD(), inParentRef,
112+
inKind, g, checkDuplicate);
109113
then
110114
g;
111115

@@ -119,6 +123,7 @@ public function mkClassNode
119123
input Ref inParentRef;
120124
input Kind inKind;
121125
input Graph inGraph;
126+
input Boolean checkDuplicate = false;
122127
output Graph outGraph;
123128
algorithm
124129
outGraph := match(inClass, inPrefix, inMod, inParentRef, inKind, inGraph)
@@ -137,7 +142,7 @@ algorithm
137142
SCode.CLASS(name = name) = cls;
138143
(g, n) = FGraph.node(g, name, {inParentRef}, FCore.CL(cls, inPrefix, inMod, inKind, FCore.CLS_UNTYPED()));
139144
nr = FNode.toRef(n);
140-
FNode.addChildRef(inParentRef, name, nr);
145+
FNode.addChildRef(inParentRef, name, nr, checkDuplicate);
141146
// g = mkRefNode(FNode.refNodeName, {}, nr, g);
142147
then
143148
g;

Compiler/FFrontEnd/FNode.mo

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ public function addChildRef
330330
input Ref inParentRef;
331331
input Name inName;
332332
input Ref inChildRef;
333+
input Boolean checkDuplicate = false;
333334
protected
334335
Name n;
335336
Integer i;
@@ -339,7 +340,8 @@ protected
339340
Ref parent;
340341
algorithm
341342
FCore.N(n, i, p, c, d) := fromRef(inParentRef);
342-
c := RefTree.add(c, inName, inChildRef, printElementConflictError);
343+
c := RefTree.add(c, inName, inChildRef,
344+
if checkDuplicate then printElementConflictError else RefTree.addConflictReplace);
343345
parent := updateRef(inParentRef, FCore.N(n, i, p, c, d));
344346
FGraphStream.edge(inName, fromRef(parent), fromRef(inChildRef));
345347
end addChildRef;
@@ -352,11 +354,14 @@ protected
352354
SourceInfo info1, info2;
353355
String name;
354356
algorithm
355-
dummy := newRef;
356-
//(name, info1) := SCode.elementNameInfo(FNode.getElementFromRef(newRef));
357-
//info2 := SCode.elementInfo(FNode.getElementFromRef(oldRef));
358-
//Error.addMultiSourceMessage(Error.DOUBLE_DECLARATION_OF_ELEMENTS, {name}, {info2, info1});
359-
//fail();
357+
if Config.acceptMetaModelicaGrammar() then
358+
dummy := newRef;
359+
else
360+
(name, info1) := SCode.elementNameInfo(FNode.getElementFromRef(newRef));
361+
info2 := SCode.elementInfo(FNode.getElementFromRef(oldRef));
362+
Error.addMultiSourceMessage(Error.DOUBLE_DECLARATION_OF_ELEMENTS, {name}, {info2, info1});
363+
fail();
364+
end if;
360365
end printElementConflictError;
361366

362367
public function addImportToRef

Compiler/FrontEnd/Inst.mo

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2011,7 +2011,8 @@ algorithm
20112011

20122012
extendselts = SCodeUtil.addRedeclareAsElementsToExtends(extendselts, List.select(els, SCodeUtil.isRedeclareElement));
20132013

2014-
(cache, env1,ih) = InstUtil.addClassdefsToEnv(cache, env, ih, pre, cdefelts, impl, SOME(mods));
2014+
(cache, env1,ih) = InstUtil.addClassdefsToEnv(cache, env, ih, pre,
2015+
cdefelts, impl, SOME(mods), FGraph.isEmptyScope(env));
20152016

20162017
//// fprintln(Flags.INST_TRACE, "after InstUtil.addClassdefsToEnv ENV: " + if_(stringEq(className, "PortVolume"), FGraph.printGraphStr(env1), " no env print "));
20172018

@@ -2849,7 +2850,7 @@ algorithm
28492850

28502851
// Classes and imports are added to env.
28512852
(outCache, outEnv, outIH) := InstUtil.addClassdefsToEnv(inCache, inEnv,
2852-
inIH, inPrefix, cdef_els, true, SOME(inMod));
2853+
inIH, inPrefix, cdef_els, true, SOME(inMod), FGraph.isEmptyScope(inEnv));
28532854
// Inherited elements are added to env.
28542855
(outCache, outEnv, outIH, emods, ext_comps) := InstExtends.instExtendsAndClassExtendsList(
28552856
outCache, outEnv, outIH, inMod, inPrefix, extends_els, class_ext_els,

Compiler/FrontEnd/InstUtil.mo

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,13 +2196,14 @@ public function addClassdefsToEnv
21962196
input list<SCode.Element> inClasses;
21972197
input Boolean inImpl;
21982198
input Option<DAE.Mod> inRedeclareMod;
2199+
input Boolean checkDuplicates = false;
21992200
output FCore.Cache outCache = inCache;
22002201
output FCore.Graph outEnv = inEnv;
22012202
output InnerOuter.InstHierarchy outIH = inIH;
22022203
algorithm
22032204
for c in inClasses loop
22042205
(outCache, outEnv, outIH) :=
2205-
addClassdefToEnv(outCache, outEnv, outIH, inPrefix, c, inImpl, inRedeclareMod);
2206+
addClassdefToEnv(outCache, outEnv, outIH, inPrefix, c, inImpl, inRedeclareMod, checkDuplicates);
22062207
end for;
22072208
end addClassdefsToEnv;
22082209

@@ -2216,6 +2217,7 @@ protected function addClassdefToEnv
22162217
input SCode.Element inSCodeElement;
22172218
input Boolean inBoolean;
22182219
input Option<DAE.Mod> redeclareMod;
2220+
input Boolean checkDuplicates = false;
22192221
output FCore.Cache outCache;
22202222
output FCore.Graph outEnv;
22212223
output InnerOuter.InstHierarchy outIH;
@@ -2252,7 +2254,7 @@ algorithm
22522254
case (cache,env,ih,pre,(sel1 as SCode.CLASS()),_)
22532255
equation
22542256
// Debug.traceln("Extend frame " + FGraph.printGraphPathStr(env) + " with " + SCode.className(cl));
2255-
env_1 = FGraph.mkClassNode(env, sel1, pre, DAE.NOMOD());
2257+
env_1 = FGraph.mkClassNode(env, sel1, pre, DAE.NOMOD(), checkDuplicates);
22562258
ih = InnerOuter.addClassIfInner(sel1, pre, env_1, ih);
22572259
then
22582260
(cache,env_1,ih);

0 commit comments

Comments
 (0)