diff --git a/OMCompiler/Compiler/NFFrontEnd/NFExpression.mo b/OMCompiler/Compiler/NFFrontEnd/NFExpression.mo index a601196e8be..43425b0e4aa 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFExpression.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFExpression.mo @@ -1701,7 +1701,8 @@ public case UNBOX() then "UNBOX(" + toFlatString(exp.exp) + ")"; case BOX() then "BOX(" + toFlatString(exp.exp) + ")"; case CAST() then toFlatString(exp.exp); - case SUBSCRIPTED_EXP() then toFlatString(exp.exp) + Subscript.toFlatStringList(exp.subscripts); + + case SUBSCRIPTED_EXP() then toFlatSubscriptedString(exp.exp, exp.subscripts); case TUPLE_ELEMENT() then toFlatString(exp.tupleExp) + "[" + intString(exp.index) + "]"; case RECORD_ELEMENT() then toFlatString(exp.recordExp) + "[field: " + exp.fieldName + "]"; case MUTABLE() then toFlatString(Mutable.access(exp.exp)); @@ -1715,6 +1716,36 @@ public end match; end toFlatString; + function toFlatSubscriptedString + input Expression exp; + input list subs; + output String str; + protected + Type exp_ty; + list sub_tyl; + list dims; + list strl; + String name; + algorithm + exp_ty := typeOf(exp); + dims := List.firstN(Type.arrayDims(exp_ty), listLength(subs)); + sub_tyl := list(Dimension.subscriptType(d) for d in dims); + name := Type.subscriptedTypeName(exp_ty, sub_tyl); + + strl := {")"}; + + for s in subs loop + strl := Subscript.toFlatString(s) :: strl; + strl := "," :: strl; + end for; + + strl := toFlatString(exp) :: strl; + strl := "'(" :: strl; + strl := name :: strl; + strl := "'" :: strl; + str := stringAppendList(strl); + end toFlatSubscriptedString; + function operandString "Helper function to toString, prints an operator and adds parentheses as needed." input Expression operand; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo b/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo index 6136c55b216..f5229a29faa 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo @@ -44,6 +44,10 @@ protected import ComplexType = NFComplexType; import NFInstNode.InstNode; import IOStream; + import NFSubscript.Subscript; + import NFClass.Class; + import NFClassTree.ClassTree; + import NFComponent.Component; import FlatModel = NFFlatModel; @@ -172,7 +176,7 @@ public end if; end for; - for ty in TypeTree.listValues(collectFlatTypes(flatModel)) loop + for ty in TypeTree.listValues(collectFlatTypes(flatModel, functions)) loop s := Type.toFlatDeclarationStream(ty, s); s := IOStream.append(s, ";\n\n"); end for; @@ -216,17 +220,19 @@ public function collectFlatTypes input FlatModel flatModel; + input list functions; output TypeTree types; algorithm types := TypeTree.new(); - types := List.fold(flatModel.variables, collectComponentFlatTypes, types); + types := List.fold(flatModel.variables, collectVariableFlatTypes, types); types := List.fold(flatModel.equations, collectEquationFlatTypes, types); types := List.fold(flatModel.initialEquations, collectEquationFlatTypes, types); types := List.fold(flatModel.algorithms, collectAlgorithmFlatTypes, types); types := List.fold(flatModel.initialAlgorithms, collectAlgorithmFlatTypes, types); + types := List.fold(functions, collectFunctionFlatTypes, types); end collectFlatTypes; - function collectComponentFlatTypes + function collectVariableFlatTypes input Variable var; input output TypeTree types; algorithm @@ -236,7 +242,7 @@ public for attr in var.typeAttributes loop types := collectBindingFlatTypes(Util.tuple22(attr), types); end for; - end collectComponentFlatTypes; + end collectVariableFlatTypes; function collectFlatType input Type ty; @@ -449,8 +455,58 @@ public input Expression exp; input output TypeTree types; algorithm - types := collectFlatType(Expression.typeOf(exp), types); + types := match exp + case Expression.SUBSCRIPTED_EXP() + algorithm + types := collectSubscriptedFlatType(exp.exp, exp.subscripts, exp.ty, types); + then + types; + + else collectFlatType(Expression.typeOf(exp), types); + end match; end collectExpFlatTypes_traverse; + function collectFunctionFlatTypes + input Function fn; + input output TypeTree types; + protected + list body; + algorithm + types := ClassTree.foldComponents(Class.classTree(InstNode.getClass(fn.node)), + collectComponentFlatTypes, types); + body := Function.getBody(fn); + types := List.fold(body, collectStatementFlatTypes, types); + end collectFunctionFlatTypes; + + function collectComponentFlatTypes + input InstNode component; + input output TypeTree types; + protected + Component comp; + algorithm + comp := InstNode.component(component); + types := collectFlatType(Component.getType(comp), types); + types := collectBindingFlatTypes(Component.getBinding(comp), types); + end collectComponentFlatTypes; + + function collectSubscriptedFlatType + input Expression exp; + input list subs; + input Type subscriptedTy; + input output TypeTree types; + protected + Type exp_ty; + list sub_tyl; + list dims; + list strl; + String name; + algorithm + exp_ty := Expression.typeOf(exp); + dims := List.firstN(Type.arrayDims(exp_ty), listLength(subs)); + sub_tyl := list(Dimension.subscriptType(d) for d in dims); + name := Type.subscriptedTypeName(exp_ty, sub_tyl); + types := TypeTree.add(types, Absyn.IDENT(name), Type.SUBSCRIPTED(name, exp_ty, sub_tyl, subscriptedTy)); + end collectSubscriptedFlatType; + annotation(__OpenModelica_Interface="frontend"); end NFFlatModel; diff --git a/OMCompiler/Compiler/NFFrontEnd/NFType.mo b/OMCompiler/Compiler/NFFrontEnd/NFType.mo index 6f3644c5053..c8e7b75651e 100644 --- a/OMCompiler/Compiler/NFFrontEnd/NFType.mo +++ b/OMCompiler/Compiler/NFFrontEnd/NFType.mo @@ -113,6 +113,13 @@ public record ANY end ANY; + record SUBSCRIPTED + String name; + Type ty; + list subs; + Type subscriptedTy; + end SUBSCRIPTED; + // TODO: Fix constants in uniontypes and use these wherever applicable to // speed up comparisons using referenceEq. //constant Type INTEGER_DEFAULT = NFType.INTEGER(); @@ -745,7 +752,7 @@ public case Type.STRING() then "String"; case Type.BOOLEAN() then "Boolean"; case Type.CLOCK() then "Clock"; - case Type.ENUMERATION() then AbsynUtil.pathString(ty.typePath); + case Type.ENUMERATION() then "'" + AbsynUtil.pathString(ty.typePath) + "'"; case Type.ENUMERATION_ANY() then "enumeration(:)"; case Type.ARRAY() then toString(ty.elementType) + "[" + stringDelimitList(List.map(ty.dimensions, Dimension.toString), ", ") + "]"; case Type.TUPLE() then "(" + stringDelimitList(List.map(ty.types, toString), ", ") + ")"; @@ -769,7 +776,10 @@ public input output IOStream.IOStream s; algorithm s := match ty - case Type.ENUMERATION() + local + Integer index; + + case ENUMERATION() algorithm s := IOStream.append(s, "type '"); s := IOStream.append(s, AbsynUtil.pathString(ty.typePath)); @@ -788,9 +798,42 @@ public then s; - case Type.COMPLEX(complexTy = ComplexType.RECORD()) + case COMPLEX(complexTy = ComplexType.RECORD()) then InstNode.toFlatStream(ty.cls, s); + case SUBSCRIPTED() + algorithm + s := IOStream.append(s, "function '"); + s := IOStream.append(s, ty.name); + s := IOStream.append(s, "'\n"); + + s := IOStream.append(s, "input "); + s := IOStream.append(s, toString(ty.ty)); + s := IOStream.append(s, " exp;\n"); + + index := 1; + for sub in ty.subs loop + s := IOStream.append(s, "input "); + s := IOStream.append(s, toString(sub)); + s := IOStream.append(s, " s"); + s := IOStream.append(s, String(index)); + s := IOStream.append(s, ";\n"); + index := index + 1; + end for; + + s := IOStream.append(s, "output "); + s := IOStream.append(s, toString(ty.subscriptedTy)); + s := IOStream.append(s, " result = exp["); + s := IOStream.append(s, + stringDelimitList(list("s" + String(i) for i in 1:listLength(ty.subs)), ",")); + s := IOStream.append(s, "];\n"); + + s := IOStream.append(s, "end '"); + s := IOStream.append(s, ty.name); + s := IOStream.append(s, "'"); + then + s; + else s; end match; end toFlatDeclarationStream; @@ -1027,5 +1070,19 @@ public end if; end sizeType; + function subscriptedTypeName + input Type expType; + input list subscriptTypes; + output String str; + protected + list strl; + algorithm + strl := list(toString(t) for t in subscriptTypes); + strl := "_" :: strl; + strl := toString(expType) :: strl; + strl := "subscript" :: strl; + str := stringAppendList(strl); + end subscriptedTypeName; + annotation(__OpenModelica_Interface="frontend"); end NFType;