Skip to content

Commit 730ed42

Browse files
committed
- Look for duplicate elements when parsing a file instead of later when it is too late (for functions) git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12924 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent c4c41f7 commit 730ed42

File tree

5 files changed

+82
-22
lines changed

5 files changed

+82
-22
lines changed

Compiler/BackEnd/BackendDAEOptimize.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,7 @@ protected function mergeAliasVars
22992299
output BackendDAE.Var outVar;
23002300
protected
23012301
BackendDAE.Var v,va,v1,v2;
2302-
Boolean fixeda, fixed,fixeda,f;
2302+
Boolean fixed,fixeda,f;
23032303
Option<DAE.Exp> sv,sva;
23042304
DAE.Exp start;
23052305
algorithm

Compiler/FrontEnd/SCode.mo

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ algorithm
668668
end match;
669669
end getElementNamed;
670670

671-
protected function getElementNamedFromElts
671+
public function getElementNamedFromElts
672672
"function: getElementNamedFromElts
673673
Helper function to getElementNamed."
674674
input Ident inIdent;
@@ -847,11 +847,32 @@ public function elementName ""
847847
output String s;
848848
algorithm
849849
s := match(e)
850-
case(COMPONENT(name = s)) then s;
851-
case(CLASS(name = s)) then s;
850+
case (COMPONENT(name = s)) then s;
851+
case (CLASS(name = s)) then s;
852852
end match;
853853
end elementName;
854854

855+
public function elementNames "Gets all elements that have an element name from the list"
856+
input list<Element> elts;
857+
output list<String> names;
858+
algorithm
859+
names := List.fold(elts,elementNamesWork,{});
860+
end elementNames;
861+
862+
protected function elementNamesWork "Gets all elements that have an element name from the list"
863+
input Element e;
864+
input list<String> acc;
865+
output list<String> out;
866+
algorithm
867+
out := match(e,acc)
868+
local
869+
String s;
870+
case (COMPONENT(name = s),acc) then s::acc;
871+
case (CLASS(name = s),acc) then s::acc;
872+
else acc;
873+
end match;
874+
end elementNamesWork;
875+
855876
public function renameElement
856877
input Element inElement;
857878
input String inName;

Compiler/FrontEnd/SCodeCheck.mo

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected import List;
5151
protected import SCodeDump;
5252
protected import Util;
5353

54-
public function checkDuplicateClasses
54+
public function checkDuplicateElements
5555
input SCode.Program inProgram;
5656
algorithm
5757
_ := match(inProgram)
@@ -61,30 +61,54 @@ algorithm
6161

6262
case (sp)
6363
equation
64-
names = List.map(sp, SCode.className);
64+
names = SCode.elementNames(sp);
6565
names = List.sort(names,Util.strcmpBool);
6666
(_,names) = Util.splitUniqueOnBool(names,stringEqual);
67-
checkForDuplicateClassesInTopScope(names);
68-
then
69-
();
67+
checkForDuplicateElements(names,sp);
68+
List.map_0(sp,checkDuplicateElementsInClass);
69+
then ();
70+
end match;
71+
end checkDuplicateElements;
72+
73+
public function checkDuplicateElementsInClass
74+
input SCode.Element el;
75+
algorithm
76+
_ := match el
77+
local
78+
SCode.Program sp;
79+
list<SCode.Element> elts;
80+
Absyn.Info info;
81+
82+
case SCode.CLASS(classDef=SCode.PARTS(elementLst=elts),info=info)
83+
equation
84+
checkDuplicateElements(elts);
85+
then ();
86+
case SCode.CLASS(classDef=SCode.CLASS_EXTENDS(composition=SCode.PARTS(elementLst=elts)),info=info)
87+
equation
88+
checkDuplicateElements(elts);
89+
then ();
90+
else ();
7091
end match;
71-
end checkDuplicateClasses;
92+
end checkDuplicateElementsInClass;
7293

73-
protected function checkForDuplicateClassesInTopScope
94+
protected function checkForDuplicateElements
7495
"Verifies that the input is empty; else an error message is printed"
7596
input list<String> duplicateNames;
97+
input SCode.Program program;
7698
algorithm
77-
_ := match duplicateNames
99+
_ := match (duplicateNames,program)
78100
local
79-
String msg;
80-
case {} then ();
81-
else
101+
String msg,id;
102+
SCode.Element elt;
103+
case ({},_) then ();
104+
case (id::_,program)
82105
equation
106+
elt = SCode.getElementNamedFromElts(id,program);
83107
msg = stringDelimitList(duplicateNames, ",");
84-
Error.addMessage(Error.DUPLICATE_CLASSES_TOP_LEVEL,{msg});
108+
Error.addSourceMessage(Error.DOUBLE_DECLARATION_OF_ELEMENTS,{msg},SCode.elementInfo(elt));
85109
then fail();
86110
end match;
87-
end checkForDuplicateClassesInTopScope;
111+
end checkForDuplicateElements;
88112

89113
public function checkRecursiveShortDefinition
90114
input Absyn.TypeSpec inTypeSpec;

Compiler/FrontEnd/SCodeUtil.mo

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,9 @@ algorithm
112112
// ModelicaBuiltin.mo and MetaModelicaBuiltin.mo
113113

114114
// check duplicates in builtin (initial) functions
115-
SCodeCheck.checkDuplicateClasses(spInitial);
115+
SCodeCheck.checkDuplicateElements(spInitial);
116116
// check duplicates in absyn
117-
SCodeCheck.checkDuplicateClasses(spAbsyn);
117+
SCodeCheck.checkDuplicateElements(spAbsyn);
118118

119119
sp = listAppend(spInitial, spAbsyn);
120120
then
@@ -1099,8 +1099,7 @@ algorithm
10991099
e_1 = translateElement(e, vis);
11001100
es_1 = translateEitemlist(es, vis);
11011101
l = listAppend(e_1, es_1);
1102-
then
1103-
l;
1102+
then l;
11041103

11051104
case ((Absyn.LEXER_COMMENT(comment = _) :: es),vis)
11061105
then translateEitemlist(es, vis);
@@ -1112,6 +1111,22 @@ algorithm
11121111
end match;
11131112
end translateEitemlist;
11141113

1114+
protected function checkForDuplicateElements
1115+
"Verifies that the input is empty; else an error message is printed"
1116+
input list<String> duplicateNames;
1117+
algorithm
1118+
_ := match duplicateNames
1119+
local
1120+
String msg;
1121+
case {} then ();
1122+
else
1123+
equation
1124+
msg = stringDelimitList(duplicateNames, ",");
1125+
Error.addMessage(Error.DOUBLE_DECLARATION_OF_ELEMENTS,{msg});
1126+
then fail();
1127+
end match;
1128+
end checkForDuplicateElements;
1129+
11151130
// stefan
11161131
protected function translateAnnotations
11171132
"function: translateAnnotations

Compiler/Util/Name.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ protected
145145
NameInteger n2i;
146146
IntegerName i2n;
147147
Integer seqNo;
148-
String str, str1, str2;
148+
String str1, str2;
149149
algorithm
150150
NAMES(n2i, i2n, seqNo) := inNames;
151151
str1 := AvlTree.prettyPrintTreeStr(n2i);

0 commit comments

Comments
 (0)