OMCompiler/Compiler/NFFrontEnd/NFInstContext.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 NFInstContext | ||
| 37 | "Used by the instantation to keep track of in which context the instantiation is done." | ||
| 38 | |||
| 39 | import NFInstNode.InstNode; | ||
| 40 | |||
| 41 | protected | ||
| 42 | import Restriction = NFRestriction; | ||
| 43 | |||
| 44 | public | ||
| 45 | type Type = Integer; | ||
| 46 | |||
| 47 | // Flag values: | ||
| 48 | constant Type NO_CONTEXT = 0; | ||
| 49 | // Global flags: | ||
| 50 | constant Type RELAXED = intBitLShift(1, 0); // Relaxed instantiation, used by e.g. checkModel. | ||
| 51 | constant Type INSTANCE_API = intBitLShift(1, 1); // Instantiation for the model instance API. | ||
| 52 | constant Type FAST_LOOKUP = intBitLShift(1, 2); // Only expand packages when doing lookup. | ||
| 53 | |||
| 54 | constant Type GLOBAL_FLAGS = intBitOr(RELAXED, intBitOr(INSTANCE_API, FAST_LOOKUP)); | ||
| 55 | |||
| 56 | // Scope flags: | ||
| 57 | constant Type CLASS = intBitLShift(1, 3); // In class. | ||
| 58 | constant Type FUNCTION = intBitLShift(1, 4); // In function. | ||
| 59 | constant Type REDECLARED = intBitLShift(1, 5); // In an element that will be replaced with a redeclare. | ||
| 60 | constant Type ALGORITHM = intBitLShift(1, 6); // In algorithm section. | ||
| 61 | constant Type EQUATION = intBitLShift(1, 7); // In equation section. | ||
| 62 | constant Type INITIAL = intBitLShift(1, 8); // In initial section. | ||
| 63 | constant Type LHS = intBitLShift(1, 9); // On left hand side of equality/assignment. | ||
| 64 | constant Type RHS = intBitLShift(1, 10); // On right hand side of equality/assignment. | ||
| 65 | constant Type WHEN = intBitLShift(1, 11); // In when equation/statement. | ||
| 66 | constant Type CLOCKED = intBitLShift(1, 12); // Part of a clocked when equation. | ||
| 67 | constant Type FOR = intBitLShift(1, 13); // In a for loop. | ||
| 68 | constant Type IF = intBitLShift(1, 14); // In an if equation/statement. | ||
| 69 | constant Type WHILE = intBitLShift(1, 15); // In a while loop. | ||
| 70 | constant Type NONEXPANDABLE = intBitLShift(1, 16); // In non-parameter if/for. | ||
| 71 | constant Type NOEVENT = intBitLShift(1, 17); // Wrapped by noEvent. | ||
| 72 | constant Type ITERATION_RANGE = intBitLShift(1, 18); // In range used for iteration. | ||
| 73 | constant Type DIMENSION = intBitLShift(1, 19); // In dimension. | ||
| 74 | constant Type BINDING = intBitLShift(1, 20); // In binding. | ||
| 75 | constant Type CONDITION = intBitLShift(1, 21); // In conditional expression. | ||
| 76 | constant Type SUBSCRIPT = intBitLShift(1, 22); // In subscript. | ||
| 77 | constant Type SUBEXPRESSION = intBitLShift(1, 23); // Part of a larger expression. | ||
| 78 | constant Type CONNECT = intBitLShift(1, 24); // Part of connect argument. | ||
| 79 | constant Type ASSERT = intBitLShift(1, 25); // Part of assert argument. | ||
| 80 | constant Type ANNOTATION = intBitLShift(1, 26); // Part of an annotation. | ||
| 81 | |||
| 82 | // Combined flags: | ||
| 83 | constant Type EQ_SUBEXPRESSION = intBitOr(EQUATION, SUBEXPRESSION); | ||
| 84 | constant Type VALID_TYPENAME_SCOPE = intBitOr(ITERATION_RANGE, DIMENSION); | ||
| 85 | constant Type DISCRETE_SCOPE = intBitOr(WHEN, intBitOr(INITIAL, FUNCTION)); | ||
| 86 | constant Type NON_EXP_FLAGS = intBitOr(GLOBAL_FLAGS, intBitOr(CLASS, FUNCTION)); | ||
| 87 | |||
| 88 | function set | ||
| 89 | input Type context; | ||
| 90 | input Type flag; | ||
| 91 | output Type newOrigin; | ||
| 92 | algorithm | ||
| 93 | 4035119 | newOrigin := intBitOr(context, flag); | |
| 94 | annotation(__OpenModelica_EarlyInline=true); | ||
| 95 | end set; | ||
| 96 | |||
| 97 | function unset | ||
| 98 | input Type context; | ||
| 99 | input Type flag; | ||
| 100 | output Type newOrigin; | ||
| 101 | algorithm | ||
| 102 | 1402 | newOrigin := intBitAnd(context, intBitNot(flag)); | |
| 103 | annotation(__OpenModelica_EarlyInline=true); | ||
| 104 | end unset; | ||
| 105 | |||
| 106 | function isSet | ||
| 107 | input Type context; | ||
| 108 | input Type flag; | ||
| 109 | output Boolean set; | ||
| 110 | algorithm | ||
| 111 | ✗ | set := intBitAnd(context, flag) > 0; | |
| 112 | annotation(__OpenModelica_EarlyInline=true); | ||
| 113 | end isSet; | ||
| 114 | |||
| 115 | function isNotSet | ||
| 116 | input Type context; | ||
| 117 | input Type flag; | ||
| 118 | output Boolean notSet; | ||
| 119 | algorithm | ||
| 120 | ✗ | notSet := intBitAnd(context, flag) == 0; | |
| 121 | annotation(__OpenModelica_EarlyInline=true); | ||
| 122 | end isNotSet; | ||
| 123 | |||
| 124 | function clearScopeFlags | ||
| 125 | input Type context; | ||
| 126 | output Type outContext; | ||
| 127 | algorithm | ||
| 128 | 1503556 | outContext := intBitAnd(context, GLOBAL_FLAGS); | |
| 129 | end clearScopeFlags; | ||
| 130 | |||
| 131 | function clearExpFlags | ||
| 132 | input Type context; | ||
| 133 | output Type outContext; | ||
| 134 | algorithm | ||
| 135 | 3250 | outContext := intBitAnd(context, NON_EXP_FLAGS); | |
| 136 | end clearExpFlags; | ||
| 137 | |||
| 138 | function inRelaxed | ||
| 139 | input Type context; | ||
| 140 | output Boolean res = intBitAnd(context, RELAXED) > 0; | ||
| 141 | end inRelaxed; | ||
| 142 | |||
| 143 | function inInstanceAPI | ||
| 144 | input Type context; | ||
| 145 | output Boolean res = intBitAnd(context, INSTANCE_API) > 0; | ||
| 146 | end inInstanceAPI; | ||
| 147 | |||
| 148 | function inFastLookup | ||
| 149 | input Type context; | ||
| 150 | output Boolean res = intBitAnd(context, FAST_LOOKUP) > 0; | ||
| 151 | end inFastLookup; | ||
| 152 | |||
| 153 | function inClass | ||
| 154 | input Type context; | ||
| 155 | output Boolean res = intBitAnd(context, CLASS) > 0; | ||
| 156 | end inClass; | ||
| 157 | |||
| 158 | function inFunction | ||
| 159 | input Type context; | ||
| 160 | output Boolean res = intBitAnd(context, FUNCTION) > 0; | ||
| 161 | end inFunction; | ||
| 162 | |||
| 163 | function inRedeclared | ||
| 164 | input Type context; | ||
| 165 | output Boolean res = intBitAnd(context, REDECLARED) > 0; | ||
| 166 | end inRedeclared; | ||
| 167 | |||
| 168 | function inAlgorithm | ||
| 169 | input Type context; | ||
| 170 | output Boolean res = intBitAnd(context, ALGORITHM) > 0; | ||
| 171 | end inAlgorithm; | ||
| 172 | |||
| 173 | function inEquation | ||
| 174 | input Type context; | ||
| 175 | output Boolean res = intBitAnd(context, EQUATION) > 0; | ||
| 176 | end inEquation; | ||
| 177 | |||
| 178 | function inInitial | ||
| 179 | input Type context; | ||
| 180 | output Boolean res = intBitAnd(context, INITIAL) > 0; | ||
| 181 | end inInitial; | ||
| 182 | |||
| 183 | function onLHS | ||
| 184 | input Type context; | ||
| 185 | output Boolean res = intBitAnd(context, LHS) > 0; | ||
| 186 | end onLHS; | ||
| 187 | |||
| 188 | function onRHS | ||
| 189 | input Type context; | ||
| 190 | output Boolean res = intBitAnd(context, RHS) > 0; | ||
| 191 | end onRHS; | ||
| 192 | |||
| 193 | function inWhen | ||
| 194 | input Type context; | ||
| 195 | output Boolean res = intBitAnd(context, WHEN) > 0; | ||
| 196 | end inWhen; | ||
| 197 | |||
| 198 | function inClocked | ||
| 199 | input Type context; | ||
| 200 | output Boolean res = intBitAnd(context, CLOCKED) > 0; | ||
| 201 | end inClocked; | ||
| 202 | |||
| 203 | function inFor | ||
| 204 | input Type context; | ||
| 205 | output Boolean res = intBitAnd(context, FOR) > 0; | ||
| 206 | end inFor; | ||
| 207 | |||
| 208 | function inIf | ||
| 209 | input Type context; | ||
| 210 | output Boolean res = intBitAnd(context, IF) > 0; | ||
| 211 | end inIf; | ||
| 212 | |||
| 213 | function inWhile | ||
| 214 | input Type context; | ||
| 215 | output Boolean res = intBitAnd(context, WHILE) > 0; | ||
| 216 | end inWhile; | ||
| 217 | |||
| 218 | function inNonexpandable | ||
| 219 | input Type context; | ||
| 220 | output Boolean res = intBitAnd(context, NONEXPANDABLE) > 0; | ||
| 221 | end inNonexpandable; | ||
| 222 | |||
| 223 | function inIterationRange | ||
| 224 | input Type context; | ||
| 225 | output Boolean res = intBitAnd(context, ITERATION_RANGE) > 0; | ||
| 226 | end inIterationRange; | ||
| 227 | |||
| 228 | function inDimension | ||
| 229 | input Type context; | ||
| 230 | output Boolean res = intBitAnd(context, DIMENSION) > 0; | ||
| 231 | end inDimension; | ||
| 232 | |||
| 233 | function inBinding | ||
| 234 | input Type context; | ||
| 235 | output Boolean res = intBitAnd(context, BINDING) > 0; | ||
| 236 | end inBinding; | ||
| 237 | |||
| 238 | function inCondition | ||
| 239 | input Type context; | ||
| 240 | output Boolean res = intBitAnd(context, CONDITION) > 0; | ||
| 241 | end inCondition; | ||
| 242 | |||
| 243 | function inSubscript | ||
| 244 | input Type context; | ||
| 245 | output Boolean res = intBitAnd(context, SUBSCRIPT) > 0; | ||
| 246 | end inSubscript; | ||
| 247 | |||
| 248 | function inSubexpression | ||
| 249 | input Type context; | ||
| 250 | output Boolean res = intBitAnd(context, SUBEXPRESSION) > 0; | ||
| 251 | end inSubexpression; | ||
| 252 | |||
| 253 | function inConnect | ||
| 254 | input Type context; | ||
| 255 | output Boolean res = intBitAnd(context, CONNECT) > 0; | ||
| 256 | end inConnect; | ||
| 257 | |||
| 258 | function inNoEvent | ||
| 259 | input Type context; | ||
| 260 | output Boolean res = intBitAnd(context, NOEVENT) > 0; | ||
| 261 | end inNoEvent; | ||
| 262 | |||
| 263 | function inAssert | ||
| 264 | input Type context; | ||
| 265 | output Boolean res = intBitAnd(context, ASSERT) > 0; | ||
| 266 | end inAssert; | ||
| 267 | |||
| 268 | function inAnnotation | ||
| 269 | input Type context; | ||
| 270 | output Boolean res = intBitAnd(context, ANNOTATION) > 0; | ||
| 271 | end inAnnotation; | ||
| 272 | |||
| 273 | function inValidTypenameScope | ||
| 274 | input Type context; | ||
| 275 | output Boolean res = intBitAnd(context, intBitOr(ITERATION_RANGE, DIMENSION)) > 0; | ||
| 276 | end inValidTypenameScope; | ||
| 277 | |||
| 278 | function inDiscreteScope | ||
| 279 | input Type context; | ||
| 280 | output Boolean res = intBitAnd(context, intBitOr(WHEN, intBitOr(INITIAL, FUNCTION))) > 0; | ||
| 281 | end inDiscreteScope; | ||
| 282 | |||
| 283 | function inLoop | ||
| 284 | input Type context; | ||
| 285 | output Boolean res = intBitAnd(context, intBitOr(FOR, WHILE)) > 0; | ||
| 286 | end inLoop; | ||
| 287 | |||
| 288 | function inValidWhenScope | ||
| 289 | input Type context; | ||
| 290 | output Boolean res = | ||
| 291 | intBitAnd(context, intBitOr(intBitOr(FUNCTION, WHILE), intBitOr(IF, intBitOr(FOR, WHEN)))) == 0; | ||
| 292 | end inValidWhenScope; | ||
| 293 | |||
| 294 | function isSingleExpression | ||
| 295 | "Returns true if the given context indicates the expression is alone on | ||
| 296 | either side of an equality/assignment." | ||
| 297 | input Type context; | ||
| 298 | output Boolean isSingle = context < ITERATION_RANGE - 1; | ||
| 299 | end isSingleExpression; | ||
| 300 | |||
| 301 | function nodeContext | ||
| 302 | "Constructs a context for a node based on where the element that the node | ||
| 303 | represents is declared rather than where it is used." | ||
| 304 | input InstNode node; | ||
| 305 | input Type currentContext; | ||
| 306 | output Type nodeContext; | ||
| 307 | protected | ||
| 308 | InstNode parent; | ||
| 309 | Restriction parent_res; | ||
| 310 | algorithm | ||
| 311 | // Copy the global flags from the current context. | ||
| 312 | 1480839 | nodeContext := clearScopeFlags(currentContext); | |
| 313 | 1480839 | parent := InstNode.explicitParent(node); | |
| 314 | |||
| 315 | // Records might be record constructors that should count as functions here, | ||
| 316 | // such record constructors are always root classes. | ||
| 317 |
2/2✓ Branch 1 taken 827793 times.
✓ Branch 2 taken 653046 times.
|
1480839 | if not InstNode.isRootClass(parent) then |
| 318 | 827793 | nodeContext := set(nodeContext, CLASS); | |
| 319 | 827793 | return; | |
| 320 | end if; | ||
| 321 | |||
| 322 | // Try to determine whether we're in a function or a class. | ||
| 323 | 653046 | parent_res := InstNode.restriction(parent); | |
| 324 |
4/4✓ Branch 1 taken 22461 times.
✓ Branch 2 taken 630585 times.
✓ Branch 4 taken 759 times.
✓ Branch 5 taken 21702 times.
|
653046 | nodeContext := if Restriction.isFunction(parent_res) or Restriction.isRecord(parent_res) then |
| 325 | set(nodeContext, FUNCTION) else set(nodeContext, CLASS); | ||
| 326 | end nodeContext; | ||
| 327 | |||
| 328 | annotation(__OpenModelica_Interface="nf_frontend"); | ||
| 329 | end NFInstContext; | ||
| 330 | |||
| 331 |