Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Absyn->SCode now checks if duplicate classes have been defined in the top-level scope
  - This includes a check for duplicate builtin classes
  - Added testcase ErrorMultipleClasses.mo
- Removed a duplicate definition in Constants.mo


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7512 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Dec 20, 2010
1 parent fc9918f commit cce7b77
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 11 deletions.
4 changes: 0 additions & 4 deletions Compiler/FrontEnd/Constants.mo
Expand Up @@ -136,10 +136,6 @@ record Bitmap
end Bitmap;
// Constants.iconProgram:
record CoordinateSystem
Real extent[2,2];
end CoordinateSystem;
record Icon
CoordinateSystem coordinateSystem(extent={{-10.0,-10.0},{10.0,10.0}});
end Icon;
Expand Down
22 changes: 22 additions & 0 deletions Compiler/FrontEnd/SCodeUtil.mo
Expand Up @@ -79,6 +79,7 @@ algorithm
InstanceHierarchy.InstanceHierarchy ih;
Boolean hasExpandableConnectors;
list<Absyn.Class> inClasses,initialClasses;
list<String> names;

case (inProgram)
equation
Expand All @@ -98,7 +99,12 @@ algorithm
System.setHasStreamConnectors(false);
sp = Util.listFold(inClasses, translate2, {});
sp = Util.listFold(initialClasses, translate2, sp);
names = Util.listMap(sp, SCode.className);
names = Util.sort(names,Util.strcmpBool);
(_,names) = Util.splitUniqueOnBool(names,stringEqual);
checkForDuplicateClassesInTopScope(names);
sp = listReverse(sp);

//sp = SCodeFlatten.flatten(sp);
//print(Util.stringDelimitList(Util.listMap(sp, SCode.printClassStr), "\n"));
// retrieve the expandable connector presence external flag
Expand Down Expand Up @@ -1848,4 +1854,20 @@ algorithm
SCode.IMPORT(imp) := elt;
end getImportFromElement;

protected function checkForDuplicateClassesInTopScope
"Verifies that the input is empty; else an error message is printed"
input list<String> duplicateNames;
algorithm
_ := match duplicateNames
local
String msg;
case {} then ();
else
equation
msg = Util.stringDelimitList(duplicateNames, ",");
Error.addMessage(Error.DUPLICATE_CLASSES_TOP_LEVEL,{msg});
then fail();
end match;
end checkForDuplicateClassesInTopScope;

end SCodeUtil;
3 changes: 3 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -241,6 +241,7 @@ public constant ErrorID RECURSIVE_SHORT_CLASS_DEFINITION=153;
public constant ErrorID FUNCTION_ELEMENT_WRONG_PROTECTION=154;
public constant ErrorID FUNCTION_ELEMENT_WRONG_KIND=155;
public constant ErrorID WITHOUT_SENDDATA=156 "Used in external C sources; do not use another index";
public constant ErrorID DUPLICATE_CLASSES_TOP_LEVEL=157;

public constant ErrorID UNBOUND_PARAMETER_WITH_START_VALUE_WARNING=499;
public constant ErrorID UNBOUND_PARAMETER_WARNING=500;
Expand Down Expand Up @@ -523,6 +524,8 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
"Expression %s in for-statement must be an array type"),
(BREAK_OUT_OF_LOOP, GRAMMAR(), WARNING(),
"Break statement found inside a loop"),
(DUPLICATE_CLASSES_TOP_LEVEL,TRANSLATION(),ERROR(),
"Duplicate classes on top level is not allowed (got %s)."),
(DUPLICATE_ELEMENTS_NOT_IDENTICAL,TRANSLATION(),ERROR(),
"Duplicate elements (due to inherited elements) not identical:\n\tfirst element is: %s\tsecond element is: %s"),
(DUPLICATE_ELEMENTS_NOT_SYNTACTICALLY_IDENTICAL,TRANSLATION(),WARNING(),
Expand Down
91 changes: 84 additions & 7 deletions Compiler/Util/Util.mo
Expand Up @@ -1146,12 +1146,28 @@ public function listMap "function: listMap
replaceable type Type_b subtypeof Any;
algorithm
/* Fastest impl. on large lists, 10M elts takes about 3 seconds */
outTypeBLst := listMap_impl_2(inTypeALst,{},inFuncTypeTypeAToTypeB);
outTypeBLst := listReverse(listMap_impl(inTypeALst,{},inFuncTypeTypeAToTypeB));
end listMap;

function listMap_impl_2
"@author adrpo
this will work in O(2n) due to listReverse"
public function listMap_reversed
"listMap, but returns a reversed list"
replaceable type TypeA subtypeof Any;
replaceable type TypeB subtypeof Any;
input list<TypeA> inLst;
input FuncTypeTypeVarToTypeVar fn;
output list<TypeB> outLst;
partial function FuncTypeTypeVarToTypeVar
input TypeA inTypeA;
output TypeB outTypeB;
replaceable type TypeA subtypeof Any;
replaceable type TypeB subtypeof Any;
end FuncTypeTypeVarToTypeVar;
algorithm
outLst := listMap_impl(inLst,{},fn);
end listMap_reversed;

protected function listMap_impl
"listMap implementation; uses an accumulator"
replaceable type TypeA subtypeof Any;
replaceable type TypeB subtypeof Any;
input list<TypeA> inLst;
Expand All @@ -1173,16 +1189,16 @@ algorithm
list<TypeB> l, result;

// revese at the end
case ({}, l, _) then listReverse(l);
case ({}, l, _) then l;
// accumulate in front
case (hd::rest, l, fn)
equation
hdChanged = fn(hd);
result = listMap_impl_2(rest, hdChanged::l, fn);
result = listMap_impl(rest, hdChanged::l, fn);
then
result;
end matchcontinue;
end listMap_impl_2;
end listMap_impl;

public function listMap_2 "function listMap_2
Takes a list and a function over the elements returning a tuple of
Expand Down Expand Up @@ -7265,4 +7281,65 @@ algorithm
end matchcontinue;
end buildMapStr;

public function splitUniqueOnBool
"Takes a sorted list and returns two sorted lists:
* The first is the input with all duplicate elements removed
* The second is the removed elements
"
input list<TypeA> sorted;
input Comp comp;
output list<TypeA> uniqueLst;
output list<TypeA> duplicateLst;
replaceable type TypeA subtypeof Any;
partial function Comp
input TypeA a1;
input TypeA a2;
output Boolean b;
end Comp;
algorithm
(uniqueLst,duplicateLst) := splitUniqueOnBoolWork(sorted,comp,{},{});
end splitUniqueOnBool;


protected function splitUniqueOnBoolWork
"Takes a sorted list and returns two sorted lists:
* The first is the input with all duplicate elements removed
* The second is the removed elements
"
input list<TypeA> sorted;
input Comp comp;
input list<TypeA> uniqueAcc;
input list<TypeA> duplicateAcc;
output list<TypeA> uniqueLst;
output list<TypeA> duplicateLst;
replaceable type TypeA subtypeof Any;
partial function Comp
input TypeA a1;
input TypeA a2;
output Boolean b;
end Comp;
algorithm
(uniqueLst,duplicateLst) := matchcontinue (sorted,comp,uniqueAcc,duplicateAcc)
local
TypeA a1,a2;
list<TypeA> rest;
Boolean b;
case ({},comp,uniqueAcc,duplicateAcc)
equation
uniqueAcc = listReverse(uniqueAcc);
duplicateAcc = listReverse(duplicateAcc);
then (uniqueAcc,duplicateAcc);
case ({a1},comp,uniqueAcc,duplicateAcc)
equation
uniqueAcc = listReverse(a1::uniqueAcc);
duplicateAcc = listReverse(duplicateAcc);
then (uniqueAcc,duplicateAcc);
case (a1::a2::rest,comp,uniqueAcc,duplicateAcc)
equation
b = comp(a1,a2);
(uniqueAcc,duplicateAcc) = splitUniqueOnBoolWork(a2::rest,comp,if_(b,uniqueAcc,a1::uniqueAcc),if_(b,a1::duplicateAcc,duplicateAcc));
then (uniqueAcc,duplicateAcc);
end matchcontinue;
end splitUniqueOnBoolWork;

end Util;

0 comments on commit cce7b77

Please sign in to comment.