OMCompiler/Compiler/FrontEnd/NFSCodeFlatten.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 NFSCodeFlatten | ||
| 37 | " file: NFSCodeFlatten.mo | ||
| 38 | package: NFSCodeFlatten | ||
| 39 | description: SCode flattening | ||
| 40 | |||
| 41 | |||
| 42 | This module flattens the SCode representation by removing all extends, imports | ||
| 43 | and redeclares, and fully qualifying class names. | ||
| 44 | " | ||
| 45 | |||
| 46 | public import Absyn; | ||
| 47 | public import AbsynUtil; | ||
| 48 | public import SCode; | ||
| 49 | public import NFSCodeDependency; | ||
| 50 | public import NFSCodeEnv; | ||
| 51 | public import NFSCodeFlattenImports; | ||
| 52 | |||
| 53 | protected import Debug; | ||
| 54 | protected import NFEnvExtends; | ||
| 55 | protected import Flags; | ||
| 56 | protected import List; | ||
| 57 | protected import System; | ||
| 58 | |||
| 59 | public type Env = NFSCodeEnv.Env; | ||
| 60 | |||
| 61 | public function flattenProgram | ||
| 62 | "Flattens the last class in a program." | ||
| 63 | input SCode.Program inProgram; | ||
| 64 | output SCode.Program outProgram; | ||
| 65 | protected | ||
| 66 | Absyn.Path cls_path; | ||
| 67 | algorithm | ||
| 68 | ✗ | cls_path := getLastClassNameInProgram(inProgram); | |
| 69 | ✗ | (outProgram, _) := flattenClassInProgram(cls_path, inProgram); | |
| 70 | end flattenProgram; | ||
| 71 | |||
| 72 | protected function getLastClassNameInProgram | ||
| 73 | "Returns the name of the last class in the program." | ||
| 74 | input SCode.Program inProgram; | ||
| 75 | output Absyn.Path outClassName; | ||
| 76 | protected | ||
| 77 | SCode.Program prog; | ||
| 78 | String name; | ||
| 79 | algorithm | ||
| 80 | ✗ | prog := listReverse(inProgram); | |
| 81 | ✗ | SCode.CLASS(name = name) := List.find(prog, isClass); | |
| 82 | ✗ | outClassName := Absyn.IDENT(name); | |
| 83 | end getLastClassNameInProgram; | ||
| 84 | |||
| 85 | protected function isClass | ||
| 86 | "Checks if the given SCode.Class is a class, i.e. not a function." | ||
| 87 | input SCode.Element inClass; | ||
| 88 | output Boolean outIsClass; | ||
| 89 | algorithm | ||
| 90 | outIsClass := match inClass | ||
| 91 | case SCode.CLASS(restriction = SCode.R_FUNCTION(_)) then false; | ||
| 92 | else true; | ||
| 93 | end match; | ||
| 94 | end isClass; | ||
| 95 | |||
| 96 | public function flattenClass | ||
| 97 | "Flattens a single class." | ||
| 98 | input SCode.Element inClass; | ||
| 99 | output SCode.Element outClass; | ||
| 100 | algorithm | ||
| 101 | ✗ | {outClass} := flattenProgram({inClass}); | |
| 102 | end flattenClass; | ||
| 103 | |||
| 104 | public function flattenClassInProgram | ||
| 105 | "Flattens a specific class in a program." | ||
| 106 | input Absyn.Path inClassName; | ||
| 107 | input SCode.Program inProgram; | ||
| 108 | output SCode.Program outProgram; | ||
| 109 | output Env outEnv; | ||
| 110 | algorithm | ||
| 111 | (outProgram, outEnv) := matchcontinue inProgram | ||
| 112 | local | ||
| 113 | Env env; | ||
| 114 | SCode.Program prog; | ||
| 115 | |||
| 116 | case prog | ||
| 117 | algorithm | ||
| 118 | //System.startTimer(); | ||
| 119 | 1105 | System.tmpTickResetIndex(0, NFSCodeEnv.tmpTickIndex); | |
| 120 | 1105 | System.tmpTickResetIndex(1, NFSCodeEnv.extendsTickIndex); | |
| 121 | 1105 | System.setUsesCardinality(false); | |
| 122 | // TODO: Enable this when NFSCodeEnv.tmpTickIndex is removed. | ||
| 123 | //System.tmpTickResetIndex(0, NFSCodeEnv.tmpTickIndex); | ||
| 124 | |||
| 125 | 1105 | env := NFSCodeEnv.buildInitialEnv(); | |
| 126 | 1105 | env := NFSCodeEnv.extendEnvWithClasses(prog, env); | |
| 127 | 1103 | env := NFEnvExtends.update(env); | |
| 128 | |||
| 129 | 1101 | (prog, env) := NFSCodeDependency.analyse(inClassName, env, prog); | |
| 130 | |||
| 131 |
2/2✓ Branch 1 taken 1087 times.
✓ Branch 2 taken 1 time.
|
1088 | if not Flags.isSet(Flags.SCODE_INST) then |
| 132 | 1087 | (prog, env) := NFSCodeFlattenImports.flattenProgram(prog, env); | |
| 133 | end if; | ||
| 134 | |||
| 135 | //System.stopTimer(); | ||
| 136 | //Debug.traceln("NFSCodeFlatten.flattenClassInProgram took " + | ||
| 137 | // realString(System.getTimerIntervalTime()) + " seconds"); | ||
| 138 | |||
| 139 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1087 times.
|
1088 | then |
| 140 | (prog, env); | ||
| 141 | |||
| 142 | else | ||
| 143 | algorithm | ||
| 144 |
1/2✓ Branch 1 taken 17 times.
✗ Branch 2 not taken.
|
17 | true := Flags.isSet(Flags.FAILTRACE); |
| 145 | ✗ | Debug.traceln("NFSCodeFlatten.flattenClassInProgram failed on " + | |
| 146 | AbsynUtil.pathString(inClassName)); | ||
| 147 | ✗ | then | |
| 148 | fail(); | ||
| 149 | |||
| 150 | end matchcontinue; | ||
| 151 | end flattenClassInProgram; | ||
| 152 | |||
| 153 | public function flattenCompleteProgram | ||
| 154 | input SCode.Program inProgram; | ||
| 155 | output SCode.Program outProgram; | ||
| 156 | algorithm | ||
| 157 | outProgram := matchcontinue inProgram | ||
| 158 | local | ||
| 159 | Env env; | ||
| 160 | SCode.Program prog; | ||
| 161 | |||
| 162 | case prog | ||
| 163 | algorithm | ||
| 164 | ✗ | env := NFSCodeEnv.buildInitialEnv(); | |
| 165 | ✗ | env := NFSCodeEnv.extendEnvWithClasses(prog, env); | |
| 166 | ✗ | env := NFEnvExtends.update(env); | |
| 167 | ✗ | (prog, env) := NFSCodeFlattenImports.flattenProgram(prog, env); | |
| 168 | then | ||
| 169 | prog; | ||
| 170 | |||
| 171 | else | ||
| 172 | algorithm | ||
| 173 | ✗ | true := Flags.isSet(Flags.FAILTRACE); | |
| 174 | ✗ | Debug.trace("NFSCodeFlatten.flattenCompleteProgram failed\n"); | |
| 175 | ✗ | then | |
| 176 | fail(); | ||
| 177 | |||
| 178 | end matchcontinue; | ||
| 179 | end flattenCompleteProgram; | ||
| 180 | |||
| 181 | annotation(__OpenModelica_Interface="frontend"); | ||
| 182 | end NFSCodeFlatten; | ||
| 183 |