OMCompiler/Compiler/FrontEnd/NFInstPrefix.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 NFInstPrefix | ||
| 37 | " file: NFInstPrefix.mo | ||
| 38 | package: NFInstPrefix | ||
| 39 | description: Prefix type and utilities for NFInst. | ||
| 40 | |||
| 41 | |||
| 42 | " | ||
| 43 | |||
| 44 | public import Absyn; | ||
| 45 | public import AbsynUtil; | ||
| 46 | public import DAE; | ||
| 47 | |||
| 48 | public uniontype Prefix | ||
| 49 | record EMPTY_PREFIX | ||
| 50 | Option<Absyn.Path> classPath "The path of the class the prefix originates from."; | ||
| 51 | end EMPTY_PREFIX; | ||
| 52 | |||
| 53 | record PREFIX | ||
| 54 | String name; | ||
| 55 | DAE.Dimensions dims; | ||
| 56 | Prefix restPrefix; | ||
| 57 | end PREFIX; | ||
| 58 | end Prefix; | ||
| 59 | |||
| 60 | public constant Prefix emptyPrefix = EMPTY_PREFIX(NONE()); | ||
| 61 | public constant Prefix functionPrefix = EMPTY_PREFIX(NONE()); | ||
| 62 | |||
| 63 | public function makePrefix | ||
| 64 | "Creates a new prefix with one identifier." | ||
| 65 | input String inName; | ||
| 66 | input DAE.Dimensions inDims; | ||
| 67 | output Prefix outPrefix; | ||
| 68 | algorithm | ||
| 69 | ✗ | outPrefix := PREFIX(inName, inDims, emptyPrefix); | |
| 70 | end makePrefix; | ||
| 71 | |||
| 72 | public function makeEmptyPrefix | ||
| 73 | "Creates a new empty prefix with the given class path." | ||
| 74 | input Absyn.Path inClassPath; | ||
| 75 | output Prefix outPrefix; | ||
| 76 | algorithm | ||
| 77 | ✗ | outPrefix := EMPTY_PREFIX(SOME(inClassPath)); | |
| 78 | end makeEmptyPrefix; | ||
| 79 | |||
| 80 | public function add | ||
| 81 | "Adds a new identifier to the given prefix." | ||
| 82 | input String inName; | ||
| 83 | input DAE.Dimensions inDimensions; | ||
| 84 | input Prefix inPrefix; | ||
| 85 | output Prefix outPrefix; | ||
| 86 | algorithm | ||
| 87 | ✗ | outPrefix := PREFIX(inName, inDimensions, inPrefix); | |
| 88 | end add; | ||
| 89 | |||
| 90 | public function addPath | ||
| 91 | "Prefixes the prefix with the given path." | ||
| 92 | input Absyn.Path inPath; | ||
| 93 | input Prefix inPrefix; | ||
| 94 | output Prefix outPrefix; | ||
| 95 | algorithm | ||
| 96 | ✗ | outPrefix := fromPath2(inPath, inPrefix); | |
| 97 | end addPath; | ||
| 98 | |||
| 99 | public function addOptPath | ||
| 100 | input Option<Absyn.Path> inOptPath; | ||
| 101 | input Prefix inPrefix; | ||
| 102 | output Prefix outPrefix; | ||
| 103 | algorithm | ||
| 104 | outPrefix := match inOptPath | ||
| 105 | local | ||
| 106 | Absyn.Path p; | ||
| 107 | |||
| 108 | case NONE() then inPrefix; | ||
| 109 | ✗ | case SOME(p) then addPath(p, inPrefix); | |
| 110 | |||
| 111 | end match; | ||
| 112 | end addOptPath; | ||
| 113 | |||
| 114 | public function addString | ||
| 115 | "Prefixes the prefix with the given String." | ||
| 116 | input String inName; | ||
| 117 | input Prefix inPrefix; | ||
| 118 | output Prefix outPrefix; | ||
| 119 | algorithm | ||
| 120 | ✗ | outPrefix := PREFIX(inName, {}, inPrefix); | |
| 121 | end addString; | ||
| 122 | |||
| 123 | public function addStringList | ||
| 124 | "Prefixes the prefix with the given list of strings." | ||
| 125 | input list<String> inStrings; | ||
| 126 | input Prefix inPrefix; | ||
| 127 | output Prefix outPrefix; | ||
| 128 | algorithm | ||
| 129 | ✗ | outPrefix := fromStringList2(inStrings, inPrefix); | |
| 130 | end addStringList; | ||
| 131 | |||
| 132 | public function restPrefix | ||
| 133 | "Discards the head of a prefix and returns the rest." | ||
| 134 | input Prefix inPrefix; | ||
| 135 | output Prefix outRestPrefix; | ||
| 136 | algorithm | ||
| 137 | ✗ | PREFIX(restPrefix = outRestPrefix) := inPrefix; | |
| 138 | end restPrefix; | ||
| 139 | |||
| 140 | public function firstName | ||
| 141 | "Returns the name of the first scope of the prefix, or an empty string if the | ||
| 142 | prefix is empty." | ||
| 143 | input Prefix inPrefix; | ||
| 144 | output String outStr; | ||
| 145 | algorithm | ||
| 146 | outStr := match inPrefix | ||
| 147 | local | ||
| 148 | String name; | ||
| 149 | |||
| 150 | case EMPTY_PREFIX() then ""; | ||
| 151 | case PREFIX(name = name) then name; | ||
| 152 | |||
| 153 | end match; | ||
| 154 | end firstName; | ||
| 155 | |||
| 156 | public function prefixCref | ||
| 157 | "Applies a prefix to a cref." | ||
| 158 | input DAE.ComponentRef inCref; | ||
| 159 | input Prefix inPrefix; | ||
| 160 | output DAE.ComponentRef outCref; | ||
| 161 | algorithm | ||
| 162 | outCref := match inPrefix | ||
| 163 | local | ||
| 164 | String name; | ||
| 165 | Prefix rest_prefix; | ||
| 166 | DAE.ComponentRef cref; | ||
| 167 | |||
| 168 | case EMPTY_PREFIX() then inCref; | ||
| 169 | |||
| 170 | case PREFIX(name = name, restPrefix = rest_prefix) | ||
| 171 | algorithm | ||
| 172 | ✗ | cref := DAE.CREF_QUAL(name, DAE.T_UNKNOWN_DEFAULT, {}, inCref); | |
| 173 | ✗ | then | |
| 174 | prefixCref(cref, rest_prefix); | ||
| 175 | |||
| 176 | end match; | ||
| 177 | end prefixCref; | ||
| 178 | |||
| 179 | public function prefixPath | ||
| 180 | "Applies a prefix to a path." | ||
| 181 | input Absyn.Path inPath; | ||
| 182 | input Prefix inPrefix; | ||
| 183 | output Absyn.Path outPath; | ||
| 184 | algorithm | ||
| 185 | outPath := match inPrefix | ||
| 186 | local | ||
| 187 | String name; | ||
| 188 | Prefix rest_prefix; | ||
| 189 | Absyn.Path path; | ||
| 190 | |||
| 191 | case EMPTY_PREFIX() then inPath; | ||
| 192 | |||
| 193 | case PREFIX(name = name, restPrefix = rest_prefix) | ||
| 194 | algorithm | ||
| 195 | ✗ | path := Absyn.QUALIFIED(name, inPath); | |
| 196 | ✗ | then | |
| 197 | prefixPath(path, rest_prefix); | ||
| 198 | |||
| 199 | end match; | ||
| 200 | end prefixPath; | ||
| 201 | |||
| 202 | public function prefixStr | ||
| 203 | "Applies a prefix to a string, using dot-notation." | ||
| 204 | input String inString; | ||
| 205 | input Prefix inPrefix; | ||
| 206 | output String outString; | ||
| 207 | algorithm | ||
| 208 | outString := match inPrefix | ||
| 209 | local | ||
| 210 | String str; | ||
| 211 | |||
| 212 | case EMPTY_PREFIX() then inString; | ||
| 213 | |||
| 214 | else | ||
| 215 | algorithm | ||
| 216 | ✗ | str := toStr(inPrefix); | |
| 217 | ✗ | str := str + "." + inString; | |
| 218 | then | ||
| 219 | str; | ||
| 220 | |||
| 221 | end match; | ||
| 222 | end prefixStr; | ||
| 223 | |||
| 224 | public function toCref | ||
| 225 | "Converts a prefix to an untyped DAE.ComponentRef." | ||
| 226 | input Prefix inPrefix; | ||
| 227 | output DAE.ComponentRef outCref; | ||
| 228 | algorithm | ||
| 229 | outCref := match inPrefix | ||
| 230 | local | ||
| 231 | String name; | ||
| 232 | Prefix rest_prefix; | ||
| 233 | DAE.ComponentRef cref; | ||
| 234 | |||
| 235 | case PREFIX(name = name, restPrefix = EMPTY_PREFIX()) | ||
| 236 | ✗ | then | |
| 237 | DAE.CREF_IDENT(name, DAE.T_UNKNOWN_DEFAULT, {}); | ||
| 238 | |||
| 239 | case PREFIX(name = name, restPrefix = rest_prefix) | ||
| 240 | algorithm | ||
| 241 | ✗ | cref := DAE.CREF_IDENT(name, DAE.T_UNKNOWN_DEFAULT, {}); | |
| 242 | ✗ | then | |
| 243 | prefixCref(cref, rest_prefix); | ||
| 244 | |||
| 245 | end match; | ||
| 246 | end toCref; | ||
| 247 | |||
| 248 | public function toPath | ||
| 249 | "Converts a prefix to a path." | ||
| 250 | input Prefix inPrefix; | ||
| 251 | output Absyn.Path outPath; | ||
| 252 | algorithm | ||
| 253 | outPath := match inPrefix | ||
| 254 | local | ||
| 255 | String name; | ||
| 256 | Prefix rest_prefix; | ||
| 257 | Absyn.Path path; | ||
| 258 | |||
| 259 | case PREFIX(name = name, restPrefix = EMPTY_PREFIX()) | ||
| 260 | ✗ | then Absyn.IDENT(name); | |
| 261 | |||
| 262 | case PREFIX(name = name, restPrefix = rest_prefix) | ||
| 263 | algorithm | ||
| 264 | ✗ | path := Absyn.IDENT(name); | |
| 265 | ✗ | then | |
| 266 | prefixPath(path, rest_prefix); | ||
| 267 | |||
| 268 | end match; | ||
| 269 | end toPath; | ||
| 270 | |||
| 271 | public function fromPath | ||
| 272 | "Converts a path to a prefix." | ||
| 273 | input Absyn.Path inPath; | ||
| 274 | output Prefix outPrefix; | ||
| 275 | algorithm | ||
| 276 | ✗ | outPrefix := fromPath2(inPath, emptyPrefix); | |
| 277 | end fromPath; | ||
| 278 | |||
| 279 | protected function fromPath2 | ||
| 280 | "Helper function to fromPath." | ||
| 281 | input Absyn.Path inPath; | ||
| 282 | input Prefix inPrefix; | ||
| 283 | output Prefix outPrefix; | ||
| 284 | algorithm | ||
| 285 | outPrefix := match inPath | ||
| 286 | local | ||
| 287 | Absyn.Path path; | ||
| 288 | String name; | ||
| 289 | |||
| 290 | case Absyn.QUALIFIED(name, path) | ||
| 291 | ✗ | then fromPath2(path, PREFIX(name, {}, inPrefix)); | |
| 292 | |||
| 293 | case Absyn.IDENT(name) | ||
| 294 | ✗ | then PREFIX(name, {}, inPrefix); | |
| 295 | |||
| 296 | case Absyn.FULLYQUALIFIED(path) | ||
| 297 | ✗ | then fromPath2(path, inPrefix); | |
| 298 | |||
| 299 | end match; | ||
| 300 | end fromPath2; | ||
| 301 | |||
| 302 | public function fromStringList | ||
| 303 | "Converts a list of strings to a prefix." | ||
| 304 | input list<String> inStrings; | ||
| 305 | output Prefix outPrefix; | ||
| 306 | algorithm | ||
| 307 | ✗ | outPrefix := fromStringList2(inStrings, emptyPrefix); | |
| 308 | end fromStringList; | ||
| 309 | |||
| 310 | protected function fromStringList2 | ||
| 311 | "Helper function to fromStringList." | ||
| 312 | input list<String> inStrings; | ||
| 313 | input Prefix inPrefix; | ||
| 314 | output Prefix outPrefix; | ||
| 315 | algorithm | ||
| 316 | outPrefix := match inStrings | ||
| 317 | local | ||
| 318 | list<String> strl; | ||
| 319 | String str; | ||
| 320 | |||
| 321 | ✗ | case str :: strl then fromStringList2(strl, PREFIX(str, {}, inPrefix)); | |
| 322 | else inPrefix; | ||
| 323 | |||
| 324 | end match; | ||
| 325 | end fromStringList2; | ||
| 326 | |||
| 327 | public function toStr | ||
| 328 | "Converts a prefix to a string using dot-notation." | ||
| 329 | input Prefix inPrefix; | ||
| 330 | output String outStr; | ||
| 331 | algorithm | ||
| 332 | outStr := match inPrefix | ||
| 333 | local | ||
| 334 | String name, str; | ||
| 335 | Prefix rest_prefix; | ||
| 336 | |||
| 337 | case EMPTY_PREFIX() then ""; | ||
| 338 | |||
| 339 | case PREFIX(name = name, restPrefix = EMPTY_PREFIX()) | ||
| 340 | then name; | ||
| 341 | |||
| 342 | case PREFIX(name = name, restPrefix = rest_prefix) | ||
| 343 | algorithm | ||
| 344 | ✗ | str := toStr(rest_prefix) + "." + name; | |
| 345 | then | ||
| 346 | str; | ||
| 347 | |||
| 348 | end match; | ||
| 349 | end toStr; | ||
| 350 | |||
| 351 | public function toStrWithEmpty | ||
| 352 | "Converts a prefix to a string using dot-notation, and also prints out the | ||
| 353 | class path of the empty prefix (for e.g. debugging)." | ||
| 354 | input Prefix inPrefix; | ||
| 355 | output String outStr; | ||
| 356 | algorithm | ||
| 357 | outStr := match inPrefix | ||
| 358 | local | ||
| 359 | String name, str; | ||
| 360 | Prefix rest_prefix; | ||
| 361 | Absyn.Path path; | ||
| 362 | |||
| 363 | case EMPTY_PREFIX(classPath = NONE()) then "E()"; | ||
| 364 | |||
| 365 | case EMPTY_PREFIX(classPath = SOME(path)) | ||
| 366 | algorithm | ||
| 367 | ✗ | str := "E(" + AbsynUtil.pathLastIdent(path) + ")"; | |
| 368 | then | ||
| 369 | str; | ||
| 370 | |||
| 371 | case PREFIX(name = name, restPrefix = rest_prefix) | ||
| 372 | algorithm | ||
| 373 | ✗ | str := toStrWithEmpty(rest_prefix) + "." + name; | |
| 374 | then | ||
| 375 | str; | ||
| 376 | |||
| 377 | end match; | ||
| 378 | end toStrWithEmpty; | ||
| 379 | |||
| 380 | public function isPackagePrefix | ||
| 381 | "Returns true if the prefix is a package prefix, i.e. a prefix without an | ||
| 382 | associated class path." | ||
| 383 | input Prefix inPrefix; | ||
| 384 | output Boolean outIsPackagePrefix; | ||
| 385 | algorithm | ||
| 386 | outIsPackagePrefix := match inPrefix | ||
| 387 | local | ||
| 388 | Prefix prefix; | ||
| 389 | |||
| 390 | ✗ | case PREFIX(restPrefix = prefix) then isPackagePrefix(prefix); | |
| 391 | case EMPTY_PREFIX(classPath = NONE()) then true; | ||
| 392 | else false; | ||
| 393 | end match; | ||
| 394 | end isPackagePrefix; | ||
| 395 | |||
| 396 | public function toPackagePrefix | ||
| 397 | "Converts a prefix to a package prefix, i.e. strips it's associated class path." | ||
| 398 | input Prefix inPrefix; | ||
| 399 | output Prefix outPrefix; | ||
| 400 | algorithm | ||
| 401 | outPrefix := match inPrefix | ||
| 402 | local | ||
| 403 | String name; | ||
| 404 | DAE.Dimensions dims; | ||
| 405 | Prefix rest_prefix; | ||
| 406 | |||
| 407 | case PREFIX(name, dims, rest_prefix) | ||
| 408 | algorithm | ||
| 409 | ✗ | rest_prefix := toPackagePrefix(rest_prefix); | |
| 410 | ✗ | then | |
| 411 | PREFIX(name, dims, rest_prefix); | ||
| 412 | |||
| 413 | case EMPTY_PREFIX() then EMPTY_PREFIX(NONE()); | ||
| 414 | |||
| 415 | end match; | ||
| 416 | end toPackagePrefix; | ||
| 417 | |||
| 418 | annotation(__OpenModelica_Interface="frontend_inst"); | ||
| 419 | end NFInstPrefix; | ||
| 420 |