OMCompiler/Compiler/Script/MMToJuliaUtil.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated package MMToJuliaUtil | ||
| 37 | protected | ||
| 38 | import Absyn; | ||
| 39 | import AbsynUtil; | ||
| 40 | //import DoubleEnded; | ||
| 41 | //import Global; | ||
| 42 | //import List; | ||
| 43 | import Util; | ||
| 44 | protected | ||
| 45 | |||
| 46 | // function getAllPartsExceptRecords | ||
| 47 | // input Absyn.Class cls; | ||
| 48 | // output list<Absyn.ClassPart> parts; | ||
| 49 | // algorithm | ||
| 50 | // end getAllPartsExceptRecords; | ||
| 51 | |||
| 52 | // function getPartsThatAreRecords | ||
| 53 | // input Absyn.Class cls; | ||
| 54 | // output list<Absyn.ClassPart> parts; | ||
| 55 | // algorithm | ||
| 56 | // end getPartsThatAreRecords; | ||
| 57 | |||
| 58 | // function splitRecordsAndOtherElements | ||
| 59 | // "This functions separates the records from the other elements of a given class." | ||
| 60 | // input Absyn.Class cls; | ||
| 61 | // output list<Absyn.ClassPart> bodyWithOnlyRecords = {}; | ||
| 62 | // output list<Absyn.ClassPart> otherElements = {}; | ||
| 63 | // algorithm | ||
| 64 | // bodyWithOnlyRecords := getPartsThatAreRecords(cls); | ||
| 65 | // otherElements := getAllPartsExceptRecords(cls); | ||
| 66 | // end splitRecordsAndOtherElements; | ||
| 67 | |||
| 68 | // function restrictionIsRecord | ||
| 69 | // input Absyn.Restriction restriction; | ||
| 70 | // output Boolean isRecord; | ||
| 71 | // algorithm | ||
| 72 | // isRecord := match restriction | ||
| 73 | // case R_RECORD(__) then true; | ||
| 74 | // else false; | ||
| 75 | // end match; | ||
| 76 | // end restrictionIsRecord; | ||
| 77 | |||
| 78 | // public | ||
| 79 | // function refactorNonStandardUniontypes | ||
| 80 | // input Absyn.Program inProgram; | ||
| 81 | // output Absyn.Program outProgram; | ||
| 82 | // protected | ||
| 83 | // constant Integer UNUSED; | ||
| 84 | // Absyn.Program tmpProgram = inProgram; | ||
| 85 | // Class tmpClass; | ||
| 86 | // algorithm | ||
| 87 | // //Traverse all classes and create a package around each uniontype containing functions | ||
| 88 | // outProgram := AbsynUtil.traverseClasses(program, | ||
| 89 | // NONE(), | ||
| 90 | // createPackageAroundUniontypeIfContainsFuncs, | ||
| 91 | // UNUSED, | ||
| 92 | // true); | ||
| 93 | // //Traverse all classes and replace all uniontypes containing functions and other crap with with uniontypes containing only records. | ||
| 94 | // //AbsynUtil.traverseClasses() | ||
| 95 | // //Traverse all classes and replace all references to the old uniontype with <package>.<uniontype> instead | ||
| 96 | // //AbsynUtil.traverseClasses() | ||
| 97 | // end refactorNonStandardUniontypes; | ||
| 98 | |||
| 99 | // function refactorUniontypesWithFunctions | ||
| 100 | // input Absyn.Program inProgram; | ||
| 101 | // output Absyn.Program outProgram; | ||
| 102 | // algorithm | ||
| 103 | // //Replace all uniontype containing functions with uniontypes containing only records. | ||
| 104 | // end refactorUniontypesWithFunctions; | ||
| 105 | |||
| 106 | // function createPackageAroundUniontypeIfContainsFuncs | ||
| 107 | // input tuple<Absyn.Class, Option<Absyn.Path>, Integer> inTuple; | ||
| 108 | // output tuple<Absyn.Class, Option<Absyn.Path>, Integer> outTuple; | ||
| 109 | // protected | ||
| 110 | // Absyn.ClassDef classDef = Util.tuple31(inTuple); | ||
| 111 | // constant Boolean VISIT_PROTECTED = true; | ||
| 112 | // constant String PACKAGE_NAME = "P" + AbsynUtil.getClassName(Util.tuple31(inTuple)); | ||
| 113 | // constant Integer UNUSED = 0; | ||
| 114 | // list<Absyn.ClassPart> bodyWithOnlyRecords = {}; | ||
| 115 | // list<Absyn.ClassPart> otherElements = {}; | ||
| 116 | // algorithm | ||
| 117 | // if not AbsynUtil.isUniontype(cls) then | ||
| 118 | // classDef := Util.tuple31(inTuple); | ||
| 119 | // end if; | ||
| 120 | // (bodyWithOnlyRecords, otherElements) := splitRecordsAndOtherElements(cls); | ||
| 121 | // classDef := PARTS({} /*Assume no typevars for the package..*/, | ||
| 122 | // {}/*Class Attributes. Only for Optimica, not used*/, | ||
| 123 | // /*classParts*/ otherElements, | ||
| 124 | // {}/* Annotations, they are kept in the nested uniontype */, | ||
| 125 | // SOME("Generated top level package")/*Class comment*/); | ||
| 126 | // // From these parts we create a package and inside this package we store things accordingly | ||
| 127 | // cls := CLASS(PACKAGE_NAME, | ||
| 128 | // false, | ||
| 129 | // false, | ||
| 130 | // Absyn.R_PACKAGE(), | ||
| 131 | // packageClsDef, | ||
| 132 | // cls.info); | ||
| 133 | // outTuple := (cls, NONE(), UNUSED); | ||
| 134 | // end createPackageAroundUniontypeIfContainsFuncs; | ||
| 135 | //TODO first figure out what we should rename | ||
| 136 | |||
| 137 | public | ||
| 138 | uniontype Context | ||
| 139 | |||
| 140 | record FUNCTION | ||
| 141 | String retValsStr "Contains return values"; | ||
| 142 | end FUNCTION; | ||
| 143 | |||
| 144 | record FUNCTION_RETURN_CONTEXT | ||
| 145 | String retValsStr "Contains return values"; | ||
| 146 | String ty_str "String of the type we are currently operating on"; | ||
| 147 | end FUNCTION_RETURN_CONTEXT; | ||
| 148 | |||
| 149 | record PACKAGE | ||
| 150 | end PACKAGE; | ||
| 151 | |||
| 152 | record UNIONTYPE | ||
| 153 | String name; | ||
| 154 | end UNIONTYPE; | ||
| 155 | |||
| 156 | record NO_CONTEXT | ||
| 157 | end NO_CONTEXT; | ||
| 158 | |||
| 159 | record INPUT_CONTEXT | ||
| 160 | String ty_str; | ||
| 161 | end INPUT_CONTEXT; | ||
| 162 | |||
| 163 | record MATCH_CONTEXT | ||
| 164 | Absyn.Exp inputExp; | ||
| 165 | end MATCH_CONTEXT; | ||
| 166 | |||
| 167 | end Context; | ||
| 168 | |||
| 169 | constant Context packageContext = PACKAGE(); | ||
| 170 | constant Context noContext = NO_CONTEXT(); | ||
| 171 | constant Context functionContext = FUNCTION(""); | ||
| 172 | constant Context returnContext = FUNCTION_RETURN_CONTEXT("",""); | ||
| 173 | constant Context inputContext = INPUT_CONTEXT(""); | ||
| 174 | |||
| 175 | function makeUniontypeContext | ||
| 176 | input String name; | ||
| 177 | output Context context; | ||
| 178 | algorithm | ||
| 179 | ✗ | context := UNIONTYPE(name); | |
| 180 | end makeUniontypeContext; | ||
| 181 | |||
| 182 | function makeInputContext | ||
| 183 | input String ty_str; | ||
| 184 | output Context context; | ||
| 185 | algorithm | ||
| 186 | ✗ | context := INPUT_CONTEXT(ty_str); | |
| 187 | end makeInputContext; | ||
| 188 | |||
| 189 | function makeFunctionContext | ||
| 190 | input String returnValuesStr; | ||
| 191 | output Context context; | ||
| 192 | algorithm | ||
| 193 | ✗ | context := FUNCTION(returnValuesStr); | |
| 194 | end makeFunctionContext; | ||
| 195 | |||
| 196 | function makeFunctionReturnContext | ||
| 197 | input String returnValuesStr; | ||
| 198 | input String ty_str; | ||
| 199 | output Context context; | ||
| 200 | algorithm | ||
| 201 | ✗ | context := FUNCTION_RETURN_CONTEXT(returnValuesStr, ty_str); | |
| 202 | end makeFunctionReturnContext; | ||
| 203 | |||
| 204 | function makeMatchContext | ||
| 205 | input Absyn.Exp iExp; | ||
| 206 | output Context context; | ||
| 207 | algorithm | ||
| 208 | ✗ | context := MATCH_CONTEXT(iExp); | |
| 209 | end makeMatchContext; | ||
| 210 | |||
| 211 | function makeInputDirection | ||
| 212 | output Absyn.Direction direction; | ||
| 213 | algorithm | ||
| 214 | direction := Absyn.INPUT(); | ||
| 215 | end makeInputDirection; | ||
| 216 | |||
| 217 | function makeOutputDirection | ||
| 218 | output Absyn.Direction direction; | ||
| 219 | algorithm | ||
| 220 | direction := Absyn.OUTPUT(); | ||
| 221 | end makeOutputDirection; | ||
| 222 | |||
| 223 | function makeInputOutputDirection | ||
| 224 | output Absyn.Direction direction; | ||
| 225 | algorithm | ||
| 226 | direction := Absyn.INPUT_OUTPUT(); | ||
| 227 | end makeInputOutputDirection; | ||
| 228 | |||
| 229 | function makeBDirection | ||
| 230 | output Absyn.Direction direction; | ||
| 231 | algorithm | ||
| 232 | direction := Absyn.BIDIR(); | ||
| 233 | end makeBDirection; | ||
| 234 | |||
| 235 | function isFunctionContext | ||
| 236 | input Context givenCTX; | ||
| 237 | output Boolean isFuncCTX = false; | ||
| 238 | algorithm | ||
| 239 | isFuncCTX := match givenCTX case FUNCTION(__) then true; else false; end match; | ||
| 240 | end isFunctionContext; | ||
| 241 | |||
| 242 | function filterOnDirection | ||
| 243 | "@author johti17 | ||
| 244 | Returns a list<ElementItem>, where the direction is equal to the supplied direction or input-output direction" | ||
| 245 | input list<Absyn.ElementItem> inputs; | ||
| 246 | input Absyn.Direction direction; | ||
| 247 | output list<Absyn.ElementItem> outputs = {}; | ||
| 248 | protected | ||
| 249 | Absyn.Direction ioDirection = makeInputOutputDirection(); | ||
| 250 | Boolean directionEQ = false; | ||
| 251 | algorithm | ||
| 252 | ✗ | for i in inputs loop | |
| 253 | ✗ | directionEQ := AbsynUtil.directionEqual(direction, AbsynUtil.getDirection(i)) | |
| 254 | or AbsynUtil.directionEqual(ioDirection, AbsynUtil.getDirection(i)); | ||
| 255 | if directionEQ then | ||
| 256 | outputs := i :: outputs; | ||
| 257 | end if; | ||
| 258 | end for; | ||
| 259 | end filterOnDirection; | ||
| 260 | |||
| 261 | function elementSpecIsBIDIR | ||
| 262 | "@author:johti17" | ||
| 263 | input Absyn.ElementSpec spec; | ||
| 264 | output Boolean isBidir; | ||
| 265 | algorithm | ||
| 266 | isBidir := match spec | ||
| 267 | local Absyn.ElementAttributes attributes; | ||
| 268 | case Absyn.COMPONENTS(attributes=attributes) then | ||
| 269 | match attributes.direction | ||
| 270 | case Absyn.BIDIR() then true; | ||
| 271 | else false; | ||
| 272 | end match; | ||
| 273 | else false; | ||
| 274 | end match; | ||
| 275 | end elementSpecIsBIDIR; | ||
| 276 | |||
| 277 | function elementSpecIsOUTPUT | ||
| 278 | "@author:johti17" | ||
| 279 | input Absyn.ElementSpec spec; | ||
| 280 | output Boolean isOutput; | ||
| 281 | algorithm | ||
| 282 | isOutput := match spec | ||
| 283 | local Absyn.ElementAttributes attributes; | ||
| 284 | case Absyn.COMPONENTS(attributes=attributes) then | ||
| 285 | match attributes.direction | ||
| 286 | case Absyn.OUTPUT() then true; | ||
| 287 | else false; | ||
| 288 | end match; | ||
| 289 | else false; | ||
| 290 | end match; | ||
| 291 | end elementSpecIsOUTPUT; | ||
| 292 | |||
| 293 | function elementSpecIsOUTPUT_OR_BIDIR | ||
| 294 | "@author:johti17" | ||
| 295 | input Absyn.ElementSpec spec; | ||
| 296 | output Boolean isOutput; | ||
| 297 | algorithm | ||
| 298 | ✗ | isOutput := elementSpecIsOUTPUT(spec) or elementSpecIsBIDIR(spec); | |
| 299 | end elementSpecIsOUTPUT_OR_BIDIR; | ||
| 300 | |||
| 301 | function explicitReturnInClassPart | ||
| 302 | "@author:johti17 | ||
| 303 | Only works for Algorithms!" | ||
| 304 | input list<Absyn.ClassPart> classParts; | ||
| 305 | output Boolean existsImplicitReturn = false; | ||
| 306 | algorithm | ||
| 307 | ✗ | for cp in classParts loop | |
| 308 | existsImplicitReturn := match cp | ||
| 309 | local list<Absyn.AlgorithmItem> contents; | ||
| 310 | ✗ | case Absyn.ALGORITHMS(contents = contents) then algorithmItemsContainsReturn(contents); | |
| 311 | else false; | ||
| 312 | end match; | ||
| 313 | end for; | ||
| 314 | end explicitReturnInClassPart; | ||
| 315 | |||
| 316 | function algorithmItemsContainsReturn | ||
| 317 | "@author: johti17" | ||
| 318 | input list<Absyn.AlgorithmItem> contents; | ||
| 319 | output Boolean existsReturn = false; | ||
| 320 | algorithm | ||
| 321 | ✗ | for item in contents loop | |
| 322 | existsReturn := match item | ||
| 323 | local Absyn.Algorithm alg; | ||
| 324 | case Absyn.ALGORITHMITEM(algorithm_ = alg) then | ||
| 325 | match alg | ||
| 326 | case Absyn.ALG_RETURN(__) then true; | ||
| 327 | else false; | ||
| 328 | end match; | ||
| 329 | else false; | ||
| 330 | end match; | ||
| 331 | end for; | ||
| 332 | end algorithmItemsContainsReturn; | ||
| 333 | |||
| 334 | function mMKeywordToJLKeyword | ||
| 335 | end mMKeywordToJLKeyword; | ||
| 336 | |||
| 337 | |||
| 338 | annotation(__OpenModelica_Interface="backend_tools"); | ||
| 339 | end MMToJuliaUtil; | ||
| 340 |