OMCompiler/Compiler/FrontEnd/InstDAE.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 InstDAE | ||
| 37 | " file: InstDAE.mo | ||
| 38 | package: InstDAE | ||
| 39 | description: DAE generation | ||
| 40 | |||
| 41 | |||
| 42 | This module is responsible for generating the DAE. | ||
| 43 | |||
| 44 | " | ||
| 45 | |||
| 46 | public import Absyn; | ||
| 47 | public import ClassInf; | ||
| 48 | public import DAE; | ||
| 49 | public import FCore; | ||
| 50 | public import FGraph; | ||
| 51 | public import InnerOuter; | ||
| 52 | public import SCode; | ||
| 53 | |||
| 54 | protected import ClassInfUtil; | ||
| 55 | protected import ComponentReference; | ||
| 56 | protected import ComponentReferenceBasics; | ||
| 57 | protected import Config; | ||
| 58 | protected import DAEUtil; | ||
| 59 | protected import Debug; | ||
| 60 | protected import ElementSource; | ||
| 61 | protected import Error; | ||
| 62 | protected import Flags; | ||
| 63 | protected import InstBinding; | ||
| 64 | protected import InstUtil; | ||
| 65 | protected import List; | ||
| 66 | import SCodeUtil; | ||
| 67 | protected import Types; | ||
| 68 | protected import DAEDump; | ||
| 69 | protected import System; | ||
| 70 | protected import Util; | ||
| 71 | |||
| 72 | protected type Ident = DAE.Ident "an identifier"; | ||
| 73 | protected type InstanceHierarchy = InnerOuter.InstHierarchy "an instance hierarchy"; | ||
| 74 | protected type InstDims = list<list<DAE.Dimension>>; | ||
| 75 | |||
| 76 | public function daeDeclare | ||
| 77 | "Given a global component name, a type, and a set of attributes, this function declares a component for the DAE result. | ||
| 78 | Altough this function returns a list of DAE.Element, only one component is actually declared. | ||
| 79 | The functions daeDeclare2 and daeDeclare3 below are helper functions that perform parts of the task. | ||
| 80 | Note: Currently, this function can only declare scalar variables, i.e. the element type of an array type is used. To indicate that the variable | ||
| 81 | is an array, the InstDims attribute is used. This will need to be redesigned in the futurue, when array variables should not be flattened out in the frontend." | ||
| 82 | input FCore.Cache inCache; | ||
| 83 | input FCore.Graph inParentEnv; | ||
| 84 | input FCore.Graph inClassEnv; | ||
| 85 | input DAE.ComponentRef inComponentRef; | ||
| 86 | input ClassInf.State inState; | ||
| 87 | input DAE.Type inType; | ||
| 88 | input SCode.Attributes inAttributes; | ||
| 89 | input SCode.Visibility visibility; | ||
| 90 | input Option<DAE.Exp> inBinding; | ||
| 91 | input list<list<DAE.Dimension>> inInstDims; | ||
| 92 | input DAE.StartValue inStartValue; | ||
| 93 | input Option<DAE.VariableAttributes> inVarAttr; | ||
| 94 | input Option<SCode.Comment> inComment; | ||
| 95 | input Absyn.InnerOuter io; | ||
| 96 | input SCode.Final finalPrefix; | ||
| 97 | input DAE.ElementSource source "the origin of the element"; | ||
| 98 | input Boolean declareComplexVars "if true, declare variables for complex variables, e.g. record vars in functions"; | ||
| 99 | output DAE.DAElist outDae; | ||
| 100 | algorithm | ||
| 101 | outDae := matchcontinue(inComponentRef, inState, inType, inAttributes, visibility, inBinding, inInstDims, inStartValue, inVarAttr, inComment) | ||
| 102 | local | ||
| 103 | DAE.ConnectorType ct1; | ||
| 104 | DAE.DAElist dae; | ||
| 105 | DAE.ComponentRef vn; | ||
| 106 | DAE.VarParallelism daeParallelism; | ||
| 107 | ClassInf.State ci_state; | ||
| 108 | DAE.Type ty; | ||
| 109 | SCode.ConnectorType ct; | ||
| 110 | SCode.Visibility vis; | ||
| 111 | SCode.Variability var; | ||
| 112 | SCode.Parallelism prl; | ||
| 113 | Absyn.Direction dir; | ||
| 114 | Option<DAE.Exp> e,start; | ||
| 115 | InstDims inst_dims; | ||
| 116 | Option<DAE.VariableAttributes> dae_var_attr; | ||
| 117 | Option<SCode.Comment> comment; | ||
| 118 | SourceInfo info; | ||
| 119 | DAE.VarKind vk; | ||
| 120 | DAE.VarDirection vd; | ||
| 121 | DAE.VarVisibility vv; | ||
| 122 | |||
| 123 | case (vn, ci_state, ty, SCode.ATTR(connectorType = ct, parallelism = prl, variability = var, | ||
| 124 | direction = dir), vis, e, inst_dims, start, dae_var_attr, comment) | ||
| 125 | algorithm | ||
| 126 | 787594 | DAE.SOURCE(info,_,_,_,_,_,_) := source; | |
| 127 | 787594 | ct1 := DAEUtil.toConnectorType(ct, ci_state); | |
| 128 | 787594 | daeParallelism := DAEUtil.toDaeParallelism(vn,prl,ci_state,info); | |
| 129 | 787594 | vk := InstUtil.makeDaeVariability(var); | |
| 130 | 787594 | vd := InstUtil.makeDaeDirection(dir); | |
| 131 | 787594 | vv := InstUtil.makeDaeProt(vis); | |
| 132 | 787594 | dae_var_attr := DAEUtil.setFinalAttr(dae_var_attr, SCodeUtil.finalBool(finalPrefix)); | |
| 133 | 787594 | dae := daeDeclare2(vn, ty, ct1, vk, vd, daeParallelism, vv, e, inst_dims, start, dae_var_attr, comment, io, source, declareComplexVars); | |
| 134 | |||
| 135 | 787594 | showDAE(inCache,inParentEnv,inClassEnv,inState,dae); | |
| 136 | then | ||
| 137 | dae; | ||
| 138 | |||
| 139 | else | ||
| 140 | algorithm | ||
| 141 | ✗ | true := Flags.isSet(Flags.FAILTRACE); | |
| 142 | ✗ | Debug.trace("- Inst.daeDeclare failed\n"); | |
| 143 | ✗ | then | |
| 144 | fail(); | ||
| 145 | |||
| 146 | end matchcontinue; | ||
| 147 | end daeDeclare; | ||
| 148 | |||
| 149 | protected function showDAE | ||
| 150 | input FCore.Cache inCache; | ||
| 151 | input FCore.Graph inParentEnv; | ||
| 152 | input FCore.Graph inClassEnv; | ||
| 153 | input ClassInf.State inState; | ||
| 154 | input DAE.DAElist inDAE; | ||
| 155 | algorithm | ||
| 156 | () := matchcontinue inDAE | ||
| 157 | local | ||
| 158 | String str, sstr; | ||
| 159 | DAE.Element comp; | ||
| 160 | DAE.DAElist dae; | ||
| 161 | list<DAE.Element> els; | ||
| 162 | |||
| 163 | case _ | ||
| 164 | algorithm | ||
| 165 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 787594 times.
|
787594 | false := Flags.isSet(Flags.SHOW_DAE_GENERATION); |
| 166 | then | ||
| 167 | (); | ||
| 168 | |||
| 169 | case _ | ||
| 170 | algorithm | ||
| 171 | ✗ | els := DAEUtil.daeElements(inDAE); | |
| 172 | ✗ | sstr := ClassInfUtil.printStateStr(inState); | |
| 173 | ✗ | sstr := "'" + sstr + "'"; | |
| 174 | ✗ | comp := DAE.COMP(sstr, els, DAE.emptyElementSource, NONE()); | |
| 175 | ✗ | dae := DAE.DAE({comp}); | |
| 176 | ✗ | str := if System.getPartialInstantiation() then " partial" else " full"; | |
| 177 | ✗ | print("DAE: parent: " + FGraph.getGraphNameStr(inParentEnv) + | |
| 178 | " class: " + FGraph.getGraphNameStr(inClassEnv) + " state: " + sstr + str + "\n" + | ||
| 179 | DAEDump.dumpStr(dae, AvlTreePathFunction.Tree.EMPTY()) + "\n"); | ||
| 180 | then | ||
| 181 | (); | ||
| 182 | |||
| 183 | case _ | ||
| 184 | algorithm | ||
| 185 | ✗ | str := if System.getPartialInstantiation() then " partial" else " full"; | |
| 186 | ✗ | print("DAE: " + ClassInfUtil.printStateStr(inState) + str + " - could not print\n"); | |
| 187 | then (); | ||
| 188 | |||
| 189 | else (); | ||
| 190 | |||
| 191 | end matchcontinue; | ||
| 192 | end showDAE; | ||
| 193 | |||
| 194 | protected function daeDeclare2 | ||
| 195 | "Helper function to daeDeclare." | ||
| 196 | input DAE.ComponentRef inComponentRef; | ||
| 197 | input DAE.Type inType; | ||
| 198 | input DAE.ConnectorType inConnectorType; | ||
| 199 | input DAE.VarKind inVarKind; | ||
| 200 | input DAE.VarDirection inVarDirection; | ||
| 201 | input DAE.VarParallelism inParallelism; | ||
| 202 | input DAE.VarVisibility protection; | ||
| 203 | input Option<DAE.Exp> inExpExpOption; | ||
| 204 | input list<list<DAE.Dimension>> inInstDims; | ||
| 205 | input DAE.StartValue inStartValue; | ||
| 206 | input Option<DAE.VariableAttributes> inAttr; | ||
| 207 | input Option<SCode.Comment> inComment; | ||
| 208 | input Absyn.InnerOuter io; | ||
| 209 | input DAE.ElementSource source "the origin of the element"; | ||
| 210 | input Boolean declareComplexVars; | ||
| 211 | output DAE.DAElist outDAe; | ||
| 212 | algorithm | ||
| 213 | outDAe := matchcontinue(inComponentRef, inType, inConnectorType, inVarKind, inVarDirection, inParallelism, protection, inExpExpOption, inInstDims, inStartValue, inAttr, inComment, declareComplexVars) | ||
| 214 | local | ||
| 215 | DAE.ComponentRef vn; | ||
| 216 | DAE.ConnectorType ct; | ||
| 217 | DAE.VarKind kind; | ||
| 218 | DAE.VarDirection dir; | ||
| 219 | DAE.VarParallelism daePrl; | ||
| 220 | Option<DAE.Exp> e,start; | ||
| 221 | InstDims inst_dims; | ||
| 222 | Option<DAE.VariableAttributes> dae_var_attr; | ||
| 223 | Option<SCode.Comment> comment; | ||
| 224 | DAE.DAElist dae; | ||
| 225 | String s; | ||
| 226 | DAE.Type ty,tp; | ||
| 227 | DAE.VarVisibility prot; | ||
| 228 | list<DAE.Dimension> finst_dims; | ||
| 229 | Absyn.Path path; | ||
| 230 | DAE.Type tty; | ||
| 231 | SourceInfo info; | ||
| 232 | |||
| 233 | case (vn, DAE.T_INTEGER(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 234 | algorithm | ||
| 235 | 44776 | finst_dims := List.flatten(inst_dims); | |
| 236 | 89552 | then DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,DAE.T_INTEGER_DEFAULT,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | |
| 237 | |||
| 238 | case (vn, DAE.T_REAL(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 239 | algorithm | ||
| 240 | 432207 | finst_dims := List.flatten(inst_dims); | |
| 241 | 864414 | then DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,DAE.T_REAL_DEFAULT,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | |
| 242 | |||
| 243 | case (vn, DAE.T_BOOL(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 244 | algorithm | ||
| 245 | 67078 | finst_dims := List.flatten(inst_dims); | |
| 246 | 134156 | then DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,DAE.T_BOOL_DEFAULT,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | |
| 247 | // BTH | ||
| 248 | case (vn, DAE.T_CLOCK(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 249 | algorithm | ||
| 250 | 166 | finst_dims := List.flatten(inst_dims); | |
| 251 | 332 | then DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,DAE.T_CLOCK_DEFAULT,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | |
| 252 | |||
| 253 | case (vn, DAE.T_STRING(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 254 | algorithm | ||
| 255 | 77015 | finst_dims := List.flatten(inst_dims); | |
| 256 | 154030 | then DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,DAE.T_STRING_DEFAULT,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | |
| 257 | |||
| 258 | case (_, DAE.T_ENUMERATION(index = SOME(_)), _, _, _, _, _, _, _, _, _, _, _) | ||
| 259 | then DAE.emptyDae; | ||
| 260 | |||
| 261 | case (vn, DAE.T_CODE(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 262 | algorithm | ||
| 263 | 26261 | finst_dims := List.flatten(inst_dims); | |
| 264 | 52522 | then | |
| 265 | DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,inType,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | ||
| 266 | |||
| 267 | // We should not declare each enumeration value of an enumeration when instantiating, | ||
| 268 | // e.g Myenum my !=> constant EnumType my.enum1,... {DAE.VAR(vn, kind, dir, DAE.ENUM, e, inst_dims)} | ||
| 269 | // instantiation of complex type extending from basic type | ||
| 270 | case (vn, ty as DAE.T_ENUMERATION(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 271 | algorithm | ||
| 272 | 27235 | finst_dims := List.flatten(inst_dims); | |
| 273 | 54470 | then DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,ty,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | |
| 274 | |||
| 275 | // complex type that is ExternalObject | ||
| 276 | case (vn, ty as DAE.T_COMPLEX(complexClassType = ClassInf.EXTERNAL_OBJ(_)), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 277 | algorithm | ||
| 278 | 1039 | finst_dims := List.flatten(inst_dims); | |
| 279 | 2078 | then | |
| 280 | DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,ty,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | ||
| 281 | |||
| 282 | // instantiation of complex type extending from basic type | ||
| 283 | case (vn, DAE.T_SUBTYPE_BASIC(complexType = tp), ct, kind, dir, daePrl, prot, e, inst_dims, start, dae_var_attr, comment, _) | ||
| 284 | algorithm | ||
| 285 | 5417 | (_,dae_var_attr) := InstBinding.instDaeVariableAttributes(FCore.emptyCache(),FGraph.empty(), DAE.NOMOD(), tp, {}); | |
| 286 | 5417 | dae := daeDeclare2(vn,tp,ct,kind,dir,daePrl,prot,e,inst_dims,start,dae_var_attr,comment,io,source,declareComplexVars); | |
| 287 | then dae; | ||
| 288 | |||
| 289 | // array that extends basic type | ||
| 290 | case (vn, DAE.T_ARRAY(dims = {DAE.DIM_INTEGER()},ty = tp), ct, kind, dir, daePrl, prot, e, inst_dims, start, dae_var_attr, comment, _) | ||
| 291 | algorithm | ||
| 292 | 38 | dae := daeDeclare2(vn, tp, ct, kind, dir, daePrl, prot,e, inst_dims, start, dae_var_attr,comment,io,source,declareComplexVars); | |
| 293 | then dae; | ||
| 294 | |||
| 295 | // Arrays with unknown dimension are allowed if not expanded | ||
| 296 | case (vn, DAE.T_ARRAY(ty = tp), ct, kind, dir, daePrl, prot, e, inst_dims, start, dae_var_attr, comment, _) | ||
| 297 | algorithm | ||
| 298 | ✗ | false := Config.splitArrays(); | |
| 299 | ✗ | dae := daeDeclare2(vn, tp, ct, kind, dir, daePrl, prot,e, inst_dims, start, dae_var_attr,comment,io,source,declareComplexVars); | |
| 300 | then | ||
| 301 | dae; | ||
| 302 | |||
| 303 | // if arrays are expanded and dimension is unknown, report an error | ||
| 304 | case (vn, DAE.T_ARRAY(dims = {DAE.DIM_UNKNOWN()}), _, _, _, _, _, _, _, _, _, _, _) | ||
| 305 | algorithm | ||
| 306 | ✗ | true := Config.splitArrays(); | |
| 307 | ✗ | s := ComponentReferenceBasics.printComponentRefStr(vn); | |
| 308 | ✗ | info := ElementSource.getElementSourceFileInfo(source); | |
| 309 | ✗ | Error.addSourceMessage(Error.DIMENSION_NOT_KNOWN, {s}, info); | |
| 310 | ✗ | then | |
| 311 | fail(); | ||
| 312 | |||
| 313 | // Complex/Record components, only if declareComplexVars is true | ||
| 314 | case(vn, ty as DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(_)), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, true) | ||
| 315 | algorithm | ||
| 316 | 4608 | finst_dims := List.flatten(inst_dims); | |
| 317 | 9216 | then | |
| 318 | DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,ty,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | ||
| 319 | |||
| 320 | // MetaModelica extensions | ||
| 321 | case (vn, tty as DAE.T_FUNCTION(), ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 322 | algorithm | ||
| 323 | 375 | finst_dims := List.flatten(inst_dims); | |
| 324 | 375 | path := ComponentReference.crefToPath(vn); | |
| 325 | 375 | tty.path := path; | |
| 326 | 750 | then | |
| 327 | DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,tty,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | ||
| 328 | |||
| 329 | // MetaModelica extension | ||
| 330 | case (vn, ty, ct, kind, dir, daePrl, prot, e, inst_dims, _, dae_var_attr, comment, _) | ||
| 331 | algorithm | ||
| 332 |
2/2✓ Branch 1 taken 18574 times.
✓ Branch 2 taken 88260 times.
|
106834 | true := Config.acceptMetaModelicaGrammar(); |
| 333 |
2/2✓ Branch 1 taken 11 times.
✓ Branch 2 taken 88249 times.
|
88260 | true := Types.isBoxedType(ty); |
| 334 | 88249 | finst_dims := List.flatten(inst_dims); | |
| 335 | 176498 | then | |
| 336 | DAE.DAE({DAE.VAR(vn,kind,dir,daePrl,prot,ty,e,finst_dims,ct,source,dae_var_attr,comment,io,false)}); | ||
| 337 | /*----------------------------*/ | ||
| 338 | |||
| 339 | else DAE.emptyDae; | ||
| 340 | end matchcontinue; | ||
| 341 | end daeDeclare2; | ||
| 342 | |||
| 343 | annotation(__OpenModelica_Interface="frontend"); | ||
| 344 | end InstDAE; | ||
| 345 |