Skip to content

Commit

Permalink
Rename conflicting elements in loadClassContentString (#11613)
Browse files Browse the repository at this point in the history
Fixes #11596
  • Loading branch information
perost committed Nov 20, 2023
1 parent ad47153 commit 0b64d0d
Show file tree
Hide file tree
Showing 6 changed files with 630 additions and 54 deletions.
134 changes: 92 additions & 42 deletions OMCompiler/Compiler/FrontEnd/AbsynUtil.mo
Expand Up @@ -612,7 +612,7 @@ algorithm
end match;
end traverseClassPartBidir;

protected function traverseEquationItemListBidir<Arg>
public function traverseEquationItemListBidir<Arg>
input list<Absyn.EquationItem> inEquationItems;
input FuncType enterFunc;
input FuncType exitFunc;
Expand All @@ -628,7 +628,7 @@ algorithm
(outEquationItems, outArg) := List.map2FoldCheckReferenceEq(inEquationItems, traverseEquationItemBidir, enterFunc, exitFunc, inArg);
end traverseEquationItemListBidir;

protected function traverseAlgorithmItemListBidir<Arg>
public function traverseAlgorithmItemListBidir<Arg>
input list<Absyn.AlgorithmItem> inAlgs;
input FuncType enterFunc;
input FuncType exitFunc;
Expand Down Expand Up @@ -933,14 +933,12 @@ algorithm
annotation(__OpenModelica_EarlyInline = true);
end makeQualifiedPathFromStrings;

public function className "returns the class name of a Absyn.Class as a Absyn.Path"
public function className
"Returns the class name of a Absyn.Class."
input Absyn.Class cl;
output Absyn.Path name;
protected
String id;
output String name;
algorithm
Absyn.CLASS(name = id) := cl;
name := Absyn.IDENT(id);
Absyn.CLASS(name = name) := cl;
end className;

public function isClassNamed
Expand Down Expand Up @@ -980,6 +978,37 @@ algorithm
end match;
end elementSpecName;

public function elementItemNames
input Absyn.ElementItem item;
output list<String> names;
algorithm
names := match item
case Absyn.ElementItem.ELEMENTITEM() then elementNames(item.element);
else {};
end match;
end elementItemNames;

public function elementNames
input Absyn.Element element;
output list<String> names;
algorithm
names := match element
case Absyn.Element.ELEMENT() then elementSpecNames(element.specification);
else {};
end match;
end elementNames;

public function elementSpecNames
input Absyn.ElementSpec spec;
output list<String> names;
algorithm
names := match spec
case Absyn.ElementSpec.CLASSDEF() then {className(spec.class_)};
case Absyn.ElementSpec.COMPONENTS() then list(componentName(c) for c in spec.components);
else {};
end match;
end elementSpecNames;

public function isClassdef
input Absyn.Element inElement;
output Boolean b;
Expand Down Expand Up @@ -4343,7 +4372,7 @@ algorithm
end match;
end opIsElementWise;

protected function dummyTraverseExp<Arg>
public function dummyTraverseExp<Arg>
input Absyn.Exp inExp;
input Arg inArg;
output Absyn.Exp outExp;
Expand Down Expand Up @@ -4380,25 +4409,41 @@ algorithm
end match;
end getClassPartsInClass;

public function getElementItemsInElement
"Returns the public and protected elements in a class."
input Absyn.Element element;
output list<Absyn.ElementItem> outElements;
protected
Absyn.Class cls;
algorithm
outElements := match element
case Absyn.Element.ELEMENT(specification = Absyn.ElementSpec.CLASSDEF(class_ = cls))
then getElementItemsInClass(cls);
else {};
end match;
end getElementItemsInElement;

public function getElementItemsInClass
"Returns the public and protected elements in a class."
input Absyn.Class inClass;
output list<Absyn.ElementItem> outElements = getElementItemsInClassDef(inClass.body);
end getElementItemsInClass;

public function getElementItemsInClassDef
"Returns the public and protected elements in a class definition."
input Absyn.ClassDef classDef;
output list<Absyn.ElementItem> outElements;
algorithm
outElements := match(inClass)
local
list<Absyn.ClassPart> parts;

case Absyn.CLASS(body = Absyn.PARTS(classParts = parts))
then List.mapFlat(parts, getElementItemsInClassPart);
outElements := match classDef
case Absyn.ClassDef.PARTS()
then List.mapFlat(classDef.classParts, getElementItemsInClassPart);

case Absyn.CLASS(body = Absyn.CLASS_EXTENDS(parts = parts))
then List.mapFlat(parts, getElementItemsInClassPart);
case Absyn.ClassDef.CLASS_EXTENDS()
then List.mapFlat(classDef.parts, getElementItemsInClassPart);

else {};

end match;
end getElementItemsInClass;
end getElementItemsInClassDef;

public function getElementItemsInClassPart
"Returns the public and protected elements in a class part."
Expand Down Expand Up @@ -5275,7 +5320,7 @@ algorithm
case ((class_ :: _),_,_,_,_)
equation
print("-traverse_classes2 failed on class:");
print(AbsynUtil.pathString(AbsynUtil.className(class_)));
print(AbsynUtil.className(class_));
print("\n");
then
fail();
Expand Down Expand Up @@ -5639,35 +5684,40 @@ algorithm
end isUniontype;

public function traverseClassElements<ArgT>
input Absyn.Class inClass;
input FuncType inFunc;
input ArgT inArg;
output Absyn.Class outClass = inClass;
output ArgT outArg;
input output Absyn.Class cls;
input FuncType func;
input output ArgT arg;

partial function FuncType
input Absyn.Element inElement;
input ArgT inArg;
output Absyn.Element outElement;
output ArgT outArg;
output Boolean outContinue;
input output Absyn.Element element;
input output ArgT arg;
output Boolean outContinue;
end FuncType;
protected
Absyn.ClassDef body;
algorithm
outClass := match(outClass)
local
Absyn.ClassDef body;

case Absyn.CLASS()
algorithm
(body, outArg) := traverseClassDef(outClass.body,
function traverseClassPartElements(inFunc = inFunc), inArg);
if not referenceEq(body, outClass.body) then outClass.body := body; end if;
then
outClass;
(body, arg) := traverseClassDefElements(cls.body, func, arg);

end match;
if not referenceEq(body, cls.body) then
cls.body := body;
end if;
end traverseClassElements;

public function traverseClassDefElements<ArgT>
input output Absyn.ClassDef classDef;
input FuncType func;
input output ArgT arg;

partial function FuncType
input output Absyn.Element element;
input output ArgT arg;
output Boolean outContinue;
end FuncType;
algorithm
(classDef, arg) := traverseClassDef(classDef,
function traverseClassPartElements(inFunc = func), arg);
end traverseClassDefElements;

protected function traverseClassPartElements<ArgT>
input Absyn.ClassPart inClassPart;
input FuncType inFunc;
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/Main/Main.mo
Expand Up @@ -238,14 +238,14 @@ algorithm

case(Absyn.PROGRAM(classes=cls,within_=Absyn.WITHIN(scope)))
equation
names = List.map(cls,AbsynUtil.className);
names = list(Absyn.Path.IDENT(AbsynUtil.className(c)) for c in cls);
names = List.map1(names,AbsynUtil.joinPaths,scope);
res = "{" + stringDelimitList(list(AbsynUtil.pathString(n) for n in names),",") + "}\n";
then res;

case(Absyn.PROGRAM(classes=cls,within_=Absyn.TOP()))
equation
names = List.map(cls,AbsynUtil.className);
names = list(Absyn.Path.IDENT(AbsynUtil.className(c)) for c in cls);
res = "{" + stringDelimitList(list(AbsynUtil.pathString(n) for n in names),",") + "}\n";
then res;

Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Script/CevalScript.mo
Expand Up @@ -666,7 +666,7 @@ algorithm
case ("parseString",{Values.STRING(str1),Values.STRING(str2)})
algorithm
Absyn.PROGRAM(classes=classes,within_=within_) := Parser.parsestring(str1,str2);
paths := List.map(classes,AbsynUtil.className);
paths := list(Absyn.Path.IDENT(AbsynUtil.className(c)) for c in classes);
paths := List.map1r(paths,AbsynUtil.joinWithinPath,within_);
vals := List.map(paths,ValuesUtil.makeCodeTypeName);
then
Expand Down

0 comments on commit 0b64d0d

Please sign in to comment.