Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 46.1% 77 / 0 / 167
Functions: -% 0 / 0 / 0
Branches: -% 0 / 0 / 0

OMCompiler/Compiler/Template/CodegenXML.tpl
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 // This file defines templates for transforming flattened Modelica code to Xml code.
37 // + Optimica code to XML code
38 // @author Alachew Shitahun <alash325@student.liu.se> - Some of the template taken from CodegenC.tpl
39
40 package CodegenXML
41
42 import interface SimCodeTV;
43 import ExpressionDumpTpl;
44
45
46 /*********************************************************************
47 * SECTION: SIMULATION TARGET, ROOT TEMPLATE
48 *********************************************************************/
49
50 4 template translateModel(SimCode simCode)
51 "Generates root template for compiling a simulation of a Modelica model."
52 ::=
53 match simCode
54 case SIMCODE(modelInfo = MODELINFO(__)) then
55 let() = textFile(generateXml(simCode), '<%dotPathXml(modelInfo.name)%>.xml')
56 "" //always returns an empty result since generated texts are written to files directly
57 end translateModel;
58
59 /*********************************************************************
60 * SECTION: SIMULATION TARGET, XML FILE SPECIFIC TEMPLATES
61 *********************************************************************/
62
63 4 template generateXml(SimCode simCode)
64 "Generates XML code for simulation file."
65 ::=
66 match simCode
67 case SIMCODE(modelInfo = MODELINFO(__)) then
68 let guid = getUUIDStr()
69 let prefix='https://github.com/JModelica/JModelica/tree/master/XML'
70 <<
71 <?xml version="1.0" encoding="UTF-8"?>
72 <OpenModelicaModelDescription
73 xmlns:exp="<%prefix%>/daeExpressions.xsd"
74 xmlns:equ="<%prefix%>/daeEquations.xsd"
75 xmlns:fun="<%prefix%>/daeFunctions.xsd"
76 xmlns:opt="<%prefix%>/daeOptimization.xsd"
77 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
78 <%modelDescriptionXml(simCode,guid)%>
79 >
80
81 <%vendorAnnotationsXml(simCode)%>
82
83 <%defaultExperiment(simulationSettingsOpt)%>
84
85 <%modelVariablesXml(modelInfo)%>
86
87 <%bindingEquationsXml(modelInfo)%>
88
89 <%equationsXml(allEquations)%>
90
91 <%initialEquationsXml(modelInfo, initialEquations)%>
92
93 <%algorithmicEquationsXml(allEquations)%>
94
95 <%recordsXml(recordDecls)%>
96
97 <%functionsXml(modelInfo.functions)%>
98
99 <%objectiveFunctionXml(classAttributes, simCode)%>
100
101 </OpenModelicaModelDescription>
102 >>
103 end generateXml;
104
105 /***********************************************************************************
106 * SECTION: GENERATE XML for MODEL DESCRIPTION AND SCALAR VARIABLES IN SIMULATION FILE
107 *************************************************************************************/
108
109 4 template vendorAnnotationsXml(SimCode simCode)
110 ::=
111 match simCode
112 case SIMCODE(modelInfo = MODELINFO(varInfo = VARINFO(__))) then
113 let generationTool= 'OpenModelica Compiler <%getVersionNr()%>'
114 <<
115 <VendorAnnotations>
116 <Tool name="<%generationTool%>"> </Tool>
117 </VendorAnnotations>
118 >>
119 end vendorAnnotationsXml;
120
121 4 template modelDescriptionXml(SimCode simCode, String guid) ::=
122 match simCode
123 case SIMCODE(modelInfo = MODELINFO(varInfo = VARINFO(__))) then
124 let fmiVersion = '1.0'
125 let modelName = dotPathXml(modelInfo.name)
126 let modelIdentifier = System.stringReplace(fileNamePrefix,".", "_")
127 let description = ''
128 let author = ''
129 let version= ''
130 let generationDateAndTime = xsdateTimeXml(Util.getCurrentDateTime())
131 let variableNamingConvention= 'structured'
132 let numberOfContinuousStates =modelInfo.varInfo.numStateVars
133 let numberOfEventIndicators =modelInfo.varInfo.numZeroCrossings
134 <<
135 fmiVersion="<%fmiVersion%>"
136 modelName="<%modelName%>"
137 modelIdentifier="<%modelIdentifier%>"
138 guid="{<%guid%>}"
139 generationDateAndTime="<%generationDateAndTime%>"
140 variableNamingConvention="<%variableNamingConvention%>"
141 numberOfContinuousStates="<%numberOfContinuousStates%>"
142 numberOfEventIndicators="<%numberOfEventIndicators%>"
143 >>
144 end modelDescriptionXml;
145
146 4 template xsdateTimeXml(DateTime dt)
147 "YYYY-MM-DDThh:mm:ss"
148 ::=
149 match dt
150 case DATETIME(__) then
151 <<
152 <%year%>-<%twodigit(mon)%>-<%twodigit(mday)%>T<%twodigit(hour)%>:<%twodigit(min)%>:<%twodigit(sec)%>
153 >>
154 end xsdateTimeXml;
155
156 4 template defaultExperiment(Option<SimulationSettings> simulationSettingsOpt)
157 "Generates code for defaultExperiment file for FMU target."
158 ::=
159 match simulationSettingsOpt
160 case SOME(de as SIMULATION_SETTINGS(__)) then
161 <<
162 <DefaultExperiment startTime="<%de.startTime%>" stopTime="<%de.stopTime%>" tolerance="<%de.tolerance%>" />
163 >>
164 end defaultExperiment;
165
166 4 template modelVariablesXml(ModelInfo modelInfo)
167 "Generates XML code for ModelVariables file ."
168 ::=
169 match modelInfo
170 case MODELINFO(vars=SIMVARS(__)) then
171 <<
172 <ModelVariables>
173 <%vars.stateVars |> var => ScalarVariableXml(var) ;separator="\n"%>
174 <%vars.derivativeVars |> var => ScalarVariableXml(var) ;separator="\n"%>
175 <%vars.algVars |> var => ScalarVariableXml(var) ;separator="\n"%>
176 <%vars.discreteAlgVars |> var => ScalarVariableXml(var) ;separator="\n"%>
177 <%vars.intAlgVars |> var => ScalarVariableXml(var) ;separator="\n"%>
178 <%vars.boolAlgVars |> var => ScalarVariableXml(var) ;separator="\n"%>
179 <%/*vars.inputVars |> var => ScalarVariableXml(var) ;separator="\n"*/%>
180 <%vars.outputVars |> var => ScalarVariableXml(var) ;separator="\n"%>
181 <%vars.aliasVars |> var => ScalarVariableXml(var) ;separator="\n"%>
182 <%vars.intAliasVars |> var => ScalarVariableXml(var) ;separator="\n"%>
183 <%vars.boolAliasVars |> var => ScalarVariableXml(var) ;separator="\n"%>
184 <%vars.paramVars |> var => ScalarVariableXml(var) ;separator="\n"%>
185 <%vars.intParamVars |> var => ScalarVariableXml(var) ;separator="\n"%>
186 <%vars.boolParamVars |> var => ScalarVariableXml(var) ;separator="\n"%>
187 <%vars.stringAlgVars |> var => ScalarVariableXml(var) ; separator="\n"%>
188 <%vars.stringParamVars |> var => ScalarVariableXml(var) ; separator="\n"%>
189 <%vars.stringAliasVars |> var => ScalarVariableXml(var) ; separator="\n"%>
190 <%vars.extObjVars |> var => ScalarVariableXml(var) ; separator="\n"%>
191 <%vars.constVars |> var => ScalarVariableXml(var) ; separator="\n"%>
192 <%vars.intConstVars |> var => ScalarVariableXml(var) ; separator="\n"%>
193 <%vars.boolConstVars |> var => ScalarVariableXml(var) ; separator="\n"%>
194 <%vars.stringConstVars |> var => ScalarVariableXml(var) ; separator="\n"%>
195 </ModelVariables><%\n%>
196 >>
197 end modelVariablesXml;
198
199 194 template ScalarVariableXml(SimVar simVar)
200 "Generates XML code for ScalarVariable file ."
201 ::=
202 match simVar
203 case SIMVAR(__) then
204 <<
205 <%ScalarVariableAttributesXml(simVar)%>
206 >>
207 end ScalarVariableXml;
208
209 194 template ScalarVariableAttributesXml(SimVar simVar)
210 "Generates XML code for ScalarVariable Attribute file ."
211 ::=
212 match simVar
213 case SIMVAR(__) then
214 let valueReference = '<%System.tmpTick()%>'
215 let variability = getVariablityXml(varKind)
216 let description = if comment then 'description="<%Util.escapeModelicaStringToXmlString(comment)%>"'
217 let alias = getAliasVarXml(aliasvar)
218 let caus = getCausalityXml(causality)
219 let variableCategory = variableCategoryXml(varKind)
220 <<
221 <ScalarVariable name="<%crefStrXml(name)%>" <%description%> valueReference="<%valueReference%>" variability="<%variability%>" causality="<%caus%>" alias="<%alias%>">
222 <%ScalarVariableTypeXml(type_,unit,displayUnit, minValue, maxValue, initialValue,isFixed)%>
223 <QualifiedName>
224 <%qualifiedNamePartXml(name)%>
225 </QualifiedName>
226 <isLinearTimedVariables>
227 <TimePoint index="0" isLinear="true"/>
228 </isLinearTimedVariables>
229 <VariableCategory><%variableCategory%></VariableCategory>
230 </ScalarVariable><%\n%>
231 >>
232 end ScalarVariableAttributesXml;
233
234 109 template getCausalityXml(Option<Causality> c)
235 "Returns the Causality Attribute of ScalarVariable."
236 ::=
237 match c
238 case SOME(NONECAUS(__)) then "none"
239 case SOME(OUTPUT(__)) then "output"
240 case SOME(INPUT(__)) then "input"
241 case SOME(LOCAL(__)) then "local" // replacement for INTERNAL(__)
242 case SOME(PARAMETER(__)) then "parameter"
243 case SOME(CALCULATED_PARAMETER(__)) then "calculatedParameter"
244 else "internal" // this should never reach here, this is mainly for guarding against failures
245 //TODO causality = independent (usually it should be "time")
246 end getCausalityXml;
247
248 104 template getVariablityXml(BackendDAE.VarKind varKind)
249 "Returns the variablity Attribute of ScalarVariable."
250 ::=
251 match varKind
252 case DISCRETE(__) then "discrete"
253 case PARAM(__) then "parameter"
254 case CONST(__) then "constant"
255 else "continuous"
256 end getVariablityXml;
257
258 148 template getAliasVarXml(AliasVariable aliasvar)
259 "Returns the alias Attribute of ScalarVariable."
260 ::=
261 match aliasvar
262 case NOALIAS(__) then "noAlias"
263 case ALIAS(__) then '<%crefStrXml(varName)%>'
264 case NEGATEDALIAS(__) then '-<%crefStrXml(varName)%>'
265 else ""
266 end getAliasVarXml;
267
268 85 template variableCategoryXml(BackendDAE.VarKind varKind)
269 "Returns the variable category of ScalarVariable."
270 ::=
271 match varKind
272 case VARIABLE(__) then "algebraic"
273 case STATE(__) then "state"
274 case STATE_DER(__) then "derivative"
275 case DUMMY_DER(__) then "algebraic"
276 case DUMMY_STATE(__) then "algebraic"
277 case DISCRETE(__) then "algebraic"
278 case PARAM(__) then "independentParameter"
279 case CONST(__) then "independentConstant"
280 case EXTOBJ(__) then 'externalObject_<%dotPathXml(fullClassName)%>'
281 case JAC_VAR(__) then "jacobianVar"
282 case JAC_TMP_VAR(__) then "jacobianTmpVar"
283 else error(sourceInfo(), "Unexpected simVarTypeName varKind")
284 end variableCategoryXml;
285
286 168 template ScalarVariableTypeXml(DAE.Type type_, String unit, String displayUnit, Option<DAE.Exp> minValue, Option<DAE.Exp> maxValue, Option<DAE.Exp> initialValue, Boolean isFixed)
287 "Generates XML code for ScalarVariable Type file."
288 ::=
289 match type_
290 case T_INTEGER(__) then '<Integer <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%> <%ScalarVariableTypeMinAttribute(minValue)%> <%ScalarVariableTypeMaxAttribute(maxValue)%>/>'
291 case T_REAL(__) then '<Real <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%> <%ScalarVariableTypeMinAttribute(minValue)%> <%ScalarVariableTypeMaxAttribute(maxValue)%> <%ScalarVariableTypeRealAttributeXml(unit,displayUnit)%>/>'
292 case T_BOOL(__) then '<Boolean <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%>/>'
293 case T_STRING(__) then '<String <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%>/>'
294 case T_ENUMERATION(__) then '<Real <%ScalarVariableTypeCommonAttributeXml(initialValue,isFixed)%>/>'
295 else 'UNKOWN_TYPE'
296 end ScalarVariableTypeXml;
297
298 108 template ScalarVariableTypeCommonAttributeXml(Option<DAE.Exp> initialValue, Boolean isFixed)
299 "Generates XML code for ScalarVariable Type file ."
300 ::=
301 match initialValue
302 case SOME(exp) then if boolOr(Expression.isEvaluatedConst(exp), Expression.isCref(exp)) then 'start="<%initValXml(exp)%>" fixed="<%isFixed%>"' else ''
303 end ScalarVariableTypeCommonAttributeXml;
304
305 21 template ScalarVariableTypeMinAttribute(Option<DAE.Exp> minValue)
306 "generates code for min attribute"
307 ::=
308 match minValue
309 case SOME(exp) then 'min="<%initValXml(exp)%>"'
310 end ScalarVariableTypeMinAttribute;
311
312 ✗ template ScalarVariableTypeMaxAttribute(Option<DAE.Exp> maxValue)
313 "generates code for max attribute"
314 ::=
315 match maxValue
316 case SOME(exp) then 'max="<%initValXml(exp)%>"'
317 end ScalarVariableTypeMaxAttribute;
318
319 96 template initValXml(Exp initialValue)
320 "Returns initial value of ScalarVariable."
321 ::=
322 match initialValue
323 case ICONST(__) then integer
324 case RCONST(__) then real
325 case SCONST(__) then '&quot;<%Util.escapeModelicaStringToXmlString(string)%>&quot;'
326 case BCONST(__) then (if bool then "1" else "0")
327 case ENUM_LITERAL(__) then '<%index%>'
328 case CREF(__) then crefStrXml(componentRef)
329 else "*ERROR* initial value of unknown type"
330 end initValXml;
331
332 168 template ScalarVariableTypeRealAttributeXml(String unit, String displayUnit)
333 "Generates XML code for ScalarVariable Type Real file ."
334 ::=
335 let unit_ = if unit then 'unit="<%Util.escapeModelicaStringToXmlString(unit)%>"'
336 let displayUnit_ = if displayUnit then 'displayUnit="<%Util.escapeModelicaStringToXmlString(displayUnit)%>"'
337 <<
338 <%unit_%> <%displayUnit_%>
339 >>
340 end ScalarVariableTypeRealAttributeXml;
341
342 4 template contextCrefXml(ComponentRef cr, Context context)
343 "Generates XML code for a component reference depending on which context we're in."
344 ::=
345 match context
346 case FUNCTION_CONTEXT(__) then System.unquoteIdentifier(crefStrXml(cr))
347 else crefXml(cr)
348 end contextCrefXml;
349
350 ✗ template contextIteratorNameXml(Ident name, Context context)
351 "Generates XML code for an iterator variable."
352 ::=
353 match context
354 case FUNCTION_CONTEXT(__) then name
355 else name
356 end contextIteratorNameXml;
357
358 337 template crefXml(ComponentRef cr)
359 "Generates Xml equivalent name for component reference."
360 ::=
361 match cr
362 case CREF_IDENT(ident = "xloc") then crefStrXml(cr)
363 case CREF_IDENT(ident = "time") then '<exp:Time>time</exp:Time>'
364 case WILD(__) then ''
365 else crefToXmlStr(cr)
366 end crefXml;
367
368 731 template qualifiedNamePartXml(ComponentRef cr)
369 "Generates XML code of the Qualified name of a variable . "
370 ::=
371 match cr
372 case CREF_IDENT(__) then
373 let arrayTest = arraysubscriptsStrXml(subscriptLst)
374 if arrayTest then
375 <<
376 <exp:QualifiedNamePart name="<%ident%>">
377 <%arraysubscriptsStrXml(subscriptLst)%>
378 </exp:QualifiedNamePart>
379 >>
380 else
381 <<
382 <exp:QualifiedNamePart name="<%ident%>"/>
383 >>
384 case CREF_QUAL(ident = "$DER") then '<%qualifiedNamePartXml(componentRef)%>'
385 case CREF_QUAL(__) then
386 let arrayTest = arraysubscriptsStrXml(subscriptLst)
387 if arrayTest then
388 <<
389 <exp:QualifiedNamePart name="<%ident%>">
390 <%arraysubscriptsStrXml(subscriptLst)%>
391 <%qualifiedNamePartXml(componentRef)%>
392 </exp:QualifiedNamePart>
393 >>
394 else
395 <<
396 <exp:QualifiedNamePart name="<%ident%>"/>
397 <%qualifiedNamePartXml(componentRef)%>
398 >>
399 else "CREF_NOT_IDENT_OR_QUAL"
400
401 end qualifiedNamePartXml;
402
403 64 template arraysubscriptsStrXml(list<DAE.Subscript> subscripts)
404 "Generares XML code for subscript part of the name."
405 ::=
406 if subscripts then
407 <<
408 <exp:ArraySubscripts>
409 <%subscripts |> s => arraysubscriptStrXml(s) ;separator="\n"%>
410 </exp:ArraySubscripts>
411 >>
412 else
413 <<>>
414 end arraysubscriptsStrXml;
415
416 64 template arraysubscriptStrXml(DAE.Subscript subscript)
417 "Generates a single subscript XML code.
418 Only works for constant integer indicies."
419
420 ::=
421 match subscript
422 case INDEX(exp=ICONST(integer=i)) then
423 <<
424 <exp:IndexExpression>
425 <exp:IntegerLiteral><%i%></exp:IntegerLiteral>
426 </exp:IndexExpression>
427 >>
428 case SLICE(exp=ICONST(integer=i)) then
429 <<
430 <exp:IndexExpression>
431 <exp:IntegerLiteral><%i%></exp:IntegerLiteral>
432 </exp:IndexExpression>
433 >>
434 case WHOLEDIM(__) then "WHOLEDIM"
435 else "UNKNOWN_SUBSCRIPT"
436 end arraysubscriptStrXml;
437
438 288 template crefToXmlStr(ComponentRef cr)
439 "Helper function to crefXml"
440 ::=
441 match cr
442 case CREF_IDENT(__) then
443 <<
444 <exp:Identifier>
445 <%qualifiedNamePartXml(cr)%>
446 </exp:Identifier>
447 >>
448 case CREF_QUAL(ident = "$DER") then
449 <<
450 <exp:Der>
451 <%crefToXmlStr(componentRef)%>
452 </exp:Der>
453 >>
454 case CREF_QUAL(__) then
455 <<
456 <exp:Identifier>
457 <%qualifiedNamePartXml(cr)%>
458 </exp:Identifier>
459 >>
460 case OPTIMICA_ATTR_INST_CREF(__) then
461 <<
462 <exp:TimedVariable timePointIndex = "0">
463 <%crefToXmlStr(componentRef)%>
464 <exp:Instant><%instant%></exp:Instant>
465 </exp:TimedVariable>
466 >>
467 case WILD(__) then ''
468 else "CREF_NOT_IDENT_OR_QUAL"
469 end crefToXmlStr;
470
471 315 template crefStrXml(ComponentRef cr)
472 "Generates the name of a variable for variable name array."
473 ::=
474 match cr
475 case CREF_IDENT(__) then '<%ident%><%subscriptsStrXml(subscriptLst)%>'
476 case CREF_QUAL(ident = "$DER") then 'der(<%crefStrXml(componentRef)%>)'
477 case CREF_QUAL(ident = "$PRE") then 'pre(<%crefStrXml(componentRef)%>)'
478 case CREF_QUAL(__) then '<%ident%><%subscriptsStrXml(subscriptLst)%>.<%crefStrXml(componentRef)%>'
479 else "CREF_NOT_IDENT_OR_QUAL"
480 end crefStrXml;
481
482 ✗ template contextArrayCrefXml(ComponentRef cr, Context context)
483 "Generates XML code for an array component reference depending on the context."
484 ::=
485 match context
486 case FUNCTION_CONTEXT(__) then arrayCrefStrXml(cr)
487 else arrayCrefXmlStr(cr)
488 end contextArrayCrefXml;
489
490 ✗ template arrayCrefXmlStr(ComponentRef cr)
491 ::= '<%arrayCrefXmlStr2(cr)%>'
492 end arrayCrefXmlStr;
493
494 ✗ template arrayCrefXmlStr2(ComponentRef cr)
495 ::=
496 match cr
497 case CREF_IDENT(__) then
498 <<
499 <exp:QualifiedName>
500 <exp:QualifiedNamePart name="<%unquoteIdentifier(ident)%>">
501 >>
502 case CREF_QUAL(__) then '<%unquoteIdentifier(ident)%><%subscriptsStrXml(subscriptLst)%>$P<%arrayCrefXmlStr2(componentRef)%>testing array'
503 else "CREF_NOT_IDENT_OR_QUAL"
504 end arrayCrefXmlStr2;
505
506 ✗ template arrayCrefStrXml(ComponentRef cr)
507 ::=
508 match cr
509 case CREF_IDENT(__) then
510 <<
511 <exp:Identifier>
512 <exp:QualifiedNamepart name ="<%ident%>"/>
513 </exp:Identifier>
514 >>
515 case CREF_QUAL(__) then '<%ident%>.<%arrayCrefStrXml(componentRef)%>'
516 else "CREF_NOT_IDENT_OR_QUAL"
517 end arrayCrefStrXml;
518
519 13 template subscriptsStrXml(list<DAE.Subscript> subscripts)
520 "Generares subscript part of the name."
521 ::=
522 if subscripts then
523 '[<%subscripts |> s => subscriptStrXml(s) ;separator=","%>]'
524 end subscriptsStrXml;
525
526 13 template subscriptStrXml(DAE.Subscript subscript)
527 "Generates a single subscript.
528 Only works for constant integer indicies."
529
530 ::=
531 match subscript
532 case INDEX(exp=ICONST(integer=i)) then i
533 case SLICE(exp=ICONST(integer=i)) then i
534 case WHOLEDIM(__) then "WHOLEDIM"
535 else "UNKNOWN_SUBSCRIPT"
536 end subscriptStrXml;
537
538 ✗ template expCrefXml(DAE.Exp ecr)
539 ::=
540 match ecr
541 case CREF(__) then crefXml(componentRef)
542 case CALL(path = IDENT(name = "der"), expLst = {arg as CREF(__)}) then
543 <<
544 <exp:Der>
545 <%crefXml(arg.componentRef)%>
546 </exp:Der>
547 >>
548 else "ERROR_NOT_A_CREF"
549 end expCrefXml;
550
551 ✗ template crefFunctionNameXml(ComponentRef cr)
552 ::=
553 match cr
554 case CREF_IDENT(__) then
555 System.stringReplace(unquoteIdentifier(ident), "_", "__")
556 case CREF_QUAL(__) then
557 '<%System.stringReplace(unquoteIdentifier(ident), "_", "__")%>_<%crefFunctionNameXml(componentRef)%>'
558 end crefFunctionNameXml;
559
560 11 template dotPathXml(Absyn.Path path)
561 "Generates paths with components separated by dots."
562 ::=
563 match path
564 case QUALIFIED(__) then '<%name%>.<%dotPathXml(path)%>'
565 case IDENT(__) then name
566 case FULLYQUALIFIED(__) then dotPathXml(path)
567 end dotPathXml;
568
569 ✗ template replaceDotAndUnderscoreXml(String str)
570 "Replace _ with __ and dot in identifiers with _"
571 ::=
572 match str
573 case name then
574 let str_dots = System.stringReplace(name,".", "_")
575 let str_underscores = System.stringReplace(str_dots, "_", "__")
576 System.unquoteIdentifier(str_underscores)
577 end replaceDotAndUnderscoreXml;
578
579 7 template underscorePathXml(Absyn.Path path)
580 "Generate XML code for paths"
581 ::=
582 match path
583 case QUALIFIED(__) then
584 <<
585 <exp:QualifiedNamePart name="<%name%>"/>
586 <%underscorePathXml(path)%>
587 >>
588 case IDENT(__) then
589 <<
590 <exp:QualifiedNamePart name="<%name%>"/>
591 >>
592 case FULLYQUALIFIED(__) then
593 <<
594 <%underscorePathXml(path)%>
595 >>
596 end underscorePathXml;
597
598
599 /*****************************************************************************
600 * SECTION: GENERATE All Function IN SIMULATION FILE
601 *****************************************************************************/
602
603 4 template bindingEquationsXml(ModelInfo modelInfo)
604 "Function for Binding Equations"
605 ::=
606 match modelInfo
607 case MODELINFO(varInfo=VARINFO(numStateVars=numStateVars),vars=SIMVARS(__)) then
608 <<
609 <equ:BindingEquations>
610 <%vars.paramVars |> var => bindingEquationXml(var) ;separator="\n"%>
611 <%vars.intParamVars |> var => bindingEquationXml(var) ;separator="\n"%>
612 <%vars.boolParamVars |> var => bindingEquationXml(var) ;separator="\n"%>
613 <%vars.stringParamVars |> var => bindingEquationXml(var) ;separator="\n"%>
614 </equ:BindingEquations>
615 >>
616 end bindingEquationsXml;
617
618 85 template bindingEquationXml(SimVar var)
619 "Generate XML code for binding Equations"
620 ::=
621 match var
622 case SIMVAR(__) then
623 let varName = '<%qualifiedNamePartXml(name)%>'
624 match initialValue
625 case SOME(exp) then
626 let &varDecls = buffer "" /*BUFD*/
627 let &preExp = buffer "" /*BUFD*/
628 <<
629 <equ:BindingEquation>
630 <equ:Parameter>
631 <%varName%>
632 </equ:Parameter>
633 <equ:BindingExp>
634 <%daeExpXml(exp, contextOther, &preExp, &varDecls)%>
635 </equ:BindingExp>
636 </equ:BindingEquation><%\n%>
637 >>
638 end bindingEquationXml;
639
640 4 template equationsXml(list<SimEqSystem> allEquationsPlusWhen)
641 "Function for all equations"
642 ::=
643 let &varDecls = buffer "" /*BUFD*/
644 let jens = System.tmpTickReset(0)
645 let &tmp = buffer ""
646 let eqs = (allEquationsPlusWhen |> eq =>
647 equation_Xml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/, &tmp)
648 ;separator="\n")
649 <<
650 <equ:DynamicEquations>
651 <%&tmp%>
652 <%eqs%>
653 </equ:DynamicEquations>
654 >>
655 end equationsXml;
656
657 4 template algorithmicEquationsXml( list<SimEqSystem> allEquations)
658 "Generates XML for an equation that is an algorithm."
659 ::=
660 let &varDecls = buffer "" /*BUFD*/
661 let algs = (allEquations |> eq =>
662 equationAlgorithmXml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/)
663 ;separator="\n")
664 <<
665 <fun:Algorithm>
666 <%algs%>
667 </fun:Algorithm>
668 >>
669 end algorithmicEquationsXml;
670
671 42 template equationAlgorithmXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
672 "Generates XML for an equation that is an algorithm."
673 ::=
674 match eq
675 case SES_ALGORITHM(__) then
676 let alg =(statements |> stmt =>
677 algStatementXml(stmt, contextFunction, &varDecls /*BUFD*/)
678 ;separator="\n")
679 <<
680 <%alg%>
681 >>
682 end equationAlgorithmXml;
683
684
685 4 template initialEquationsXml(ModelInfo modelInfo, list<SimEqSystem> initialEqs)
686 "Function for Inititial Equations."
687 ::=
688 match modelInfo
689 case MODELINFO(varInfo=VARINFO(numStateVars=numStateVars),vars=SIMVARS(__)) then
690 let &varDecls = buffer "" /*BUFD*/
691 let jens = System.tmpTickReset(0)
692 let &tmp = buffer ""
693 let eqs = (initialEqs |> eq =>
694 equation_Xml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/, &tmp)
695 ;separator="\n")
696 <<
697 <equ:InitialEquations>
698 <%vars.stateVars |> var => initialEquationXml(var) ;separator="\n"%>
699 <%vars.derivativeVars |> var => initialEquationXml(var) ;separator="\n"%>
700 <%vars.algVars |> var => initialEquationXml(var) ;separator="\n"%>
701 <%vars.discreteAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
702 <%vars.intAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
703 <%vars.boolAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
704 <%vars.stringAlgVars |> var => initialEquationXml(var) ;separator="\n"%>
705 <%&tmp%>
706 <%eqs%>
707 </equ:InitialEquations>
708 >>
709 end initialEquationsXml;
710
711 62 template initialEquationXml(SimVar var)
712 "Generates XML code for Inititial Equations."
713 ::=
714 match var
715 case SIMVAR(__) then
716 let identName = '<%crefXml(name)%>'
717 match initialValue
718 case SOME(exp) then
719 let &varDecls = buffer "" /*BUFD*/
720 let &preExp = buffer "" /*BUFD*/
721 <<
722 <equ:Equation>
723 <exp:Sub>
724 <%identName%>
725 <%daeExpXml(exp, contextOther, &preExp, &varDecls)%>
726 </exp:Sub>
727 </equ:Equation><%\n%>
728 >>
729 end initialEquationXml;
730
731 /*****************************************************************************
732 * SECTION: GENERATE All EQUATIONS IN SIMULATION FILE
733 *****************************************************************************/
734
735 102 template equation_Xml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/, Text &eqs)
736 "Generates an equation.
737 This template should not be used for a SES_RESIDUAL.
738 Residual equations are handled differently."
739 ::=
740 match eq
741 case e as SES_MIXED(__) then " MIXED EQUATION NOT IMPLEMENTED "
742 case e as SES_ALGORITHM(statements={}) then " "
743 case e as SES_ALGORITHM(__) then " "
744 case e as SES_WHEN(__)
745 then equationWhenXml(e, context, &varDecls /*BUFD*/)
746 else
747 (
748 let ix = System.tmpTickIndex(10)
749 let &tmp = buffer ""
750 let &varD = buffer ""
751 let x = match eq
752 case e as SES_SIMPLE_ASSIGN(__)
753 case e as SES_SIMPLE_ASSIGN_CONSTRAINTS(__)
754 then equationSimpleAssignXml(e, context, &varD /*BUFD*/)
755 case e as SES_ARRAY_CALL_ASSIGN(__)
756 then equationArrayCallAssignXml(e, context, &varD /*BUFD*/)
757 case e as SES_IFEQUATION(__)
758 then 'IfEquation Assign Not implemente yet'
759 case e as SES_LINEAR(__)
760 then equationLinearXml(e, context, &varD /*BUFD*/)
761 case e as SES_NONLINEAR(__)
762 then equationNonlinearXml(e, context, &varD /*BUFD*/)
763 case e as SES_WHEN(__)
764 then " "
765 case e as SES_ALIAS(__)
766 then " "
767 else
768 "NOT IMPLEMENTED EQUATION"
769 let &eqs +=
770 <<
771 <equ:Equation>
772 <exp:Sub>
773 <%x%>
774 </exp:Sub>
775 </equ:Equation><%\n%>
776 >>
777 <<
778 >>
779 )
780 end equation_Xml;
781
782 ✗ template old_equation_Xml(SimEqSystem eq, Context context, Text &varDecls)
783 "Generates an equation.
784 This template should not be used for a SES_RESIDUAL.
785 Residual equations are handled differently."
786 ::=
787 match eq
788 case e as SES_MIXED(__)
789 case e as SES_SIMPLE_ASSIGN(__)
790 case e as SES_SIMPLE_ASSIGN_CONSTRAINTS(__)
791 then equationSimpleAssignXml(e, context, &varDecls)
792 case e as SES_ARRAY_CALL_ASSIGN(__)
793 then equationArrayCallAssignXml(e, context, &varDecls)
794 case e as SES_ALGORITHM(__) then " "
795 case e as SES_LINEAR(__) then " equations are not implemented yet"
796 case e as SES_NONLINEAR(__) then "equations are not implemented yet "
797 case e as SES_WHEN(__)
798 then equationWhenXml(e, context, &varDecls)
799 else
800 "NOT IMPLEMENTED EQUATION"
801 end old_equation_Xml;
802
803 75 template equationSimpleAssignXml(SimEqSystem eq, Context context,
804 Text &varDecls /*BUFP*/)
805 "Generates an equation that is just a simple assignment."
806 ::=
807 match eq
808 case SES_SIMPLE_ASSIGN(__)
809 case SES_SIMPLE_ASSIGN_CONSTRAINTS(__) then
810 let &preExp = buffer "" /*BUFD*/
811 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
812 let result = if preExp then preExp else expPart
813 <<
814 <%crefXml(cref)%>
815 <%result%>
816 >>
817 end equationSimpleAssignXml;
818
819 ✗ template equationArrayCallAssignXml(SimEqSystem eq, Context context,
820 Text &varDecls /*BUFP*/)
821 "Generates equation on form 'cref_array = call(...)'."
822 ::=
823 <<
824 <%match eq
825
826 case eqn as SES_ARRAY_CALL_ASSIGN(lhs=lhs as CREF(__)) then
827 let &preExp = buffer "" /*BUFD*/
828 let expPart = daeExpXml(exp, context, &preExp /*BUF let &preExp = buffer "" /*BUFD*/
829 //let &helpInits = buffer "" /*BUFD*/
830 //let helpIf = (conditions |> e =>
831 // let helpInit = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
832 //let &helpInits += ' <%helpInit%>'
833 // '';separator=" || ")C*/, &varDecls /*BUFD*/)
834 match expTypeFromExpShortXml(eqn.exp)
835 case "boolean" then
836 <<
837 <%expPart%>
838 <%crefXml(lhs.componentRef)%>
839 >>
840 case "integer" then
841 <<
842 <%expPart%>
843 <%crefXml(lhs.componentRef)%>
844 >>
845 case "real" then
846 <<
847 <%crefXml(lhs.componentRef)%>
848 <%expPart%>
849 >>
850 else error(sourceInfo(), 'No runtime support for this sort of array call: <%ExpressionDumpTpl.dumpExp(eqn.exp,"\"")%>')
851 %>
852 >>
853 end equationArrayCallAssignXml;
854
855
856 ✗ template equationLinearXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
857 "Generates a when equation XML."
858 ::=
859 match eq
860 case SES_LINEAR(lSystem=ls as LINEARSYSTEM(__)) then
861 <<
862 <%ls.simJac |> (row, col, eq as SES_RESIDUAL(__)) =>
863 let &preExp = buffer "" /*BUFD*/
864 let expPart = daeExpXml(eq.exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
865 '<%preExp%>
866 <%expPart%>' ;separator="\n"%>
867 <%ls.beqs |> exp hasindex i0 =>
868 let &preExp = buffer "" /*BUFD*/
869 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
870 '<%preExp%>
871 <%expPart%>' ;separator="\n"%>
872 >>
873 end equationLinearXml;
874
875 1 template equationNonlinearXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
876 "Generates a when equation XML."
877 ::=
878 match eq
879 case SES_NONLINEAR(nlSystem=nls as NONLINEARSYSTEM(__)) then
880 let &varDecls = buffer "" /*BUFD*/
881 let &tmp = buffer ""
882 let prebody = (nls.eqs |> eq2 =>
883 functionExtraResidualsPreBody(eq2, &varDecls /*BUFD*/, &tmp)
884 ;separator="\n")
885 let body = (nls.eqs |> eq2 as SES_RESIDUAL(__) hasindex i0 =>
886 let &preExp = buffer "" /*BUFD*/
887 let expPart = daeExpXml(eq2.exp, contextSimulationDiscrete,
888 &preExp /*BUFC*/, &varDecls /*BUFD*/)
889 '<%preExp%>
890 <%expPart%>;';separator="\n")
891 <<
892 <%&tmp%>
893 <%prebody%>
894 <%body%>
895 >>
896 end equationNonlinearXml;
897
898 1 template functionExtraResidualsPreBody(SimEqSystem eq, Text &varDecls /*BUFP*/, Text &eqs)
899 "Generates an equation."
900 ::=
901 match eq
902 case e as SES_RESIDUAL(__) then ""
903 else equation_Xml(eq, contextSimulationDiscrete, &varDecls /*BUFD*/, &eqs)
904 end match
905 end functionExtraResidualsPreBody;
906
907
908 1 template equationWhenXml(SimEqSystem eq, Context context, Text &varDecls /*BUFP*/)
909 "Generates a when equation XML."
910 ::=
911 match eq
912 case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = NONE()) then
913 let &preExp = buffer "" /*BUFD*/
914 let &helpInits = buffer "" /*BUFD*/
915 let helpIf = (conditions |> e =>
916 let helpInit = crefToXmlStr(e)
917 let &helpInits += '<%helpInit%><%\n%>'
918 '';separator="\n")
919 let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
920 let cond = if preExp then preExp else helpInits
921 <<
922 <equ:When>
923 <equ:Condition>
924 <%cond%>
925 </equ:Condition>
926 <equ:Equation>
927 <%body%>
928 </equ:Equation>
929 </equ:When>
930 >>
931 case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = SOME(elseWhenEq)) then
932 let &preExp = buffer "" /*BUFD*/
933 let &helpInits = buffer "" /*BUFD*/
934 let helpIf = (conditions |> e =>
935 let helpInit = crefToXmlStr(e)
936 let &helpInits += '<%helpInit%><%\n%>'
937 '';separator=" || ")
938 let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
939 let elseWhen = equationElseWhenXml(elseWhenEq,context,preExp,helpInits, varDecls)
940 let cond = if preExp then preExp else helpInits
941 <<
942 <equ:When>
943 <equ:Condition>
944 <%cond%>
945 </equ:Condition>
946 <equ:Equation>
947 <%body%>
948 </equ:Equation>
949 </equ:When>
950 <%elseWhen%>
951 >>
952 end equationWhenXml;
953
954 ✗ template equationElseWhenXml(SimEqSystem eq, Context context, Text &preExp /*BUFD*/, Text &helpInits /*BUFD*/, Text &varDecls /*BUFP*/)
955 "Generates a else when equation."
956 ::=
957 match eq
958 case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = NONE()) then
959 let helpIf = (conditions |> e =>
960 let helpInit = crefToXmlStr(e)
961 let &helpInits += '<%helpInit%><%\n%>'
962 '';separator=" || ")
963 let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
964 let cond = if preExp then preExp else helpInits
965 <<
966 <equ:ElseWhen>
967 <equ:Condition>
968 <%cond%>
969 </equ:Condition>
970 <equ:Equation>
971 <%body%>
972 </equ:Equation>
973 </equ:ElseWhen>
974 >>
975 case SES_WHEN(whenStmtLst = whenStmtLst, conditions=conditions,elseWhen = SOME(elseWhenEq)) then
976 let helpIf = (conditions |> e =>
977 let helpInit = crefToXmlStr(e)
978 let &helpInits += '<%helpInit%><%\n%>'
979 '';separator=" || ")
980 let body = whenOps(whenStmtLst, context, &varDecls /*BUFD*/)
981 let elseWhen = equationElseWhenXml(elseWhenEq,context,preExp,helpInits, varDecls)
982 let cond = if preExp then preExp else helpInits
983 <<
984 <equ:ElseWhen>
985 <equ:Condition>
986 <%cond%>
987 </equ:Condition>
988 <equ:Equation>
989 <%body%>
990 </equ:Equation>
991 </equ:ElseWhen>
992 <%elseWhen%>
993 >>
994 end equationElseWhenXml;
995
996 1 template whenOps(list<WhenOperator> whenOps, Context context, Text &varDecls /*BUFP*/)
997 "Generates re-init statement for when equation."
998 ::=
999 let body = (whenOps |> whenOp =>
1000 match whenOp
1001 case ASSIGN(left = lhls as DAE.CREF(componentRef = cr)) then
1002 let &preExp = buffer "" /*BUFD*/
1003 let exp = daeExpXml(right, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1004 <<
1005 <exp:Sub>
1006 <%crefXml(cr)%>
1007 <%exp%>
1008 </exp:Sub>
1009 >>
1010 case REINIT(__) then
1011 let &preExp = buffer "" /*BUFD*/
1012 let val = daeExpXml(value, contextSimulationDiscrete,
1013 &preExp /*BUFC*/, &varDecls /*BUFD*/)
1014 <<
1015 <exp:Reinit>
1016 <%crefXml(stateVar)%>
1017 <%val%>
1018 </exp:Reinit>
1019 >>
1020 case TERMINATE(__) then
1021 let &preExp = buffer "" /*BUFD*/
1022 let msgVar = daeExpXml(message, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1023 <<
1024 <%preExp%>
1025 <%msgVar%>
1026 >>
1027 case ASSERT(source=SOURCE(info=info)) then
1028 assertCommonXml(condition, message, contextSimulationDiscrete, &varDecls, info)
1029 ;separator="\n")
1030 <<
1031 <%body%>
1032 >>
1033 end whenOps;
1034
1035
1036 /*****************************************************************************
1037 * SECTION: GENERATE ALL RECORDS ( RECORD LIST) IN SIMULATION FILE
1038 *****************************************************************************/
1039
1040 4 template recordsXml(list<RecordDeclaration> recordDecls)
1041 "Generates XML code for all records."
1042 ::=
1043 <<
1044 <fun:RecordsList>
1045 <%recordDecls |> rd => recordDeclarationXml(rd) ;separator="\n"%>
1046 </fun:RecordsList>
1047 >>
1048 end recordsXml;
1049
1050 ✗ template recordDeclarationXml(RecordDeclaration recDecl)
1051 "Generates XML structs for a record declaration."
1052 ::=
1053 match recDecl
1054 case RECORD_DECL_FULL(__) then
1055 <<
1056 <fun:Record>
1057 <fun:Name>
1058 <exp:QualifiedNamePart name ='<%name%>'/>
1059 </fun:Name>
1060 <%variables |> var => recordBodyXml(var) ;separator="\n"%>
1061 </fun:Record>
1062 >>
1063 case RECORD_DECL_DEF(__) then
1064 <<
1065 Record Declaration definition is not yet implemented
1066 >>
1067 end recordDeclarationXml;
1068
1069 ✗ template recordBodyXml(Variable var)
1070 ::=
1071 match var
1072 case VARIABLE(ty=T_COMPLEX(complexClassType=RECORD(__))) then
1073 <<
1074 <fun:Field type="Record">
1075 <fun:Name>
1076 <exp:QualifiedNamePart name="<%contextCrefXml(name,contextFunction)%>"/>
1077 </fun:Name>
1078 <fun:Record>
1079 <%varTypeXml(var)%>
1080 </fun:Record>
1081 </fun:Field>
1082 >>
1083 case VARIABLE(__) then
1084 <<
1085 <fun:Field type="<%varTypeXml(var)%>">
1086 <fun:Name>
1087 <exp:QualifiedNamePart name="<%crefStrXml(var.name)%>"/>
1088 </fun:Name>
1089 </fun:Field>
1090 >>
1091 case FUNCTION_PTR(__) then 'modelica_fnptr <%name%>'
1092 end recordBodyXml;
1093
1094 /*********************************************************************************************
1095 * SECTION: GENERATE All USER DEFINED FUNCTIONS INCLUDING EXTERNAL FUNCTIONS IN SIMULATION FILE
1096 **********************************************************************************************/
1097
1098 4 template functionsXml(list<SimCodeFunction.Function> functions)
1099 "Generates the body for a set of functions."
1100 ::=
1101 <<
1102 <fun:FunctionsList>
1103 <%functions |> fn => functionXml(fn) ;separator="\n"%>
1104 </fun:FunctionsList>
1105 >>
1106 end functionsXml;
1107
1108 1 template functionXml(SimCodeFunction.Function fn)
1109 "Generates the body for a function."
1110 ::=
1111 match fn
1112 case fn as FUNCTION(__) then regularFunctionXml(fn)
1113 case fn as EXTERNAL_FUNCTION(__) then externalFunctionXml(fn)
1114 case fn as RECORD_CONSTRUCTOR(__) then ''
1115 end functionXml;
1116
1117 ✗ template regularFunctionXml(SimCodeFunction.Function fn)
1118 "Generates XML code for a Modelica function."
1119 ::=
1120 match fn
1121 case FUNCTION(__) then
1122 let()= System.tmpTickReset(1)
1123 let fname = underscorePathXml(name)
1124 let &varDecls = buffer "" /*BUFD*/
1125 let &varInits = buffer "" /*BUFD*/
1126 let bodyPart = funStatementXml(body, &varDecls)
1127 <<
1128 <fun:Function>
1129 <fun:Name>
1130 <%fname%>
1131 </fun:Name>
1132 <%outVars |> var => funOutputVariableXml(var) ;separator="\n"%>
1133 <%functionArguments |> var => funArgDefinitionXml(var) ;separator="\n"%>
1134 <%/*variableDeclarations |> var => funVarDeclarationsXml(var) ;separator="\n"*/%>
1135 <fun:Algorithm>
1136 <%bodyPart%>
1137 </fun:Algorithm>
1138 </fun:Function><%\n%>
1139 >>
1140 end regularFunctionXml;
1141
1142 1 template externalFunctionXml(SimCodeFunction.Function fn)
1143 "Generates the body for an external function (just a wrapper)."
1144 ::=
1145 match fn
1146 case efn as EXTERNAL_FUNCTION(__) then
1147 let()= System.tmpTickReset(1)
1148 let &preExp = buffer "" /*BUFD*/
1149 let &varDecls = buffer "" /*BUFD*/
1150 let fname = underscorePathXml(name)
1151 let callPart = extFunCallXml(fn, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1152 <<
1153 <fun:Function>
1154 <fun:Name>
1155 <%fname%>
1156 </fun:Name>
1157 <%outVars |> var => funOutputVariableXml(var) ;separator="\n"%>
1158 <%funArgs |> var => funArgDefinitionXml(var) ;separator="\n"%>
1159 <fun:Algorithm>
1160 <%callPart%>
1161 </fun:Algorithm>
1162 </fun:Function><%\n%>
1163 >>
1164 end externalFunctionXml;
1165
1166 ✗ template funArgNameXml(Variable var)
1167 ::=
1168 match var
1169 case VARIABLE(__) then contextCrefXml(name,contextFunction)
1170 case FUNCTION_PTR(__) then name
1171 end funArgNameXml;
1172
1173 1 template funOutputVariableXml(Variable var)
1174 ::=
1175 match var
1176 case VARIABLE(ty=T_COMPLEX(complexClassType=RECORD(__))) then
1177 <<
1178 <fun:OutputVariable type="Record">
1179 <fun:Name>
1180 <exp:QualifiedNamePart name="<%contextCrefXml(name,contextFunction)%>"/>
1181 </fun:Name>
1182 <fun:Record>
1183 <%varTypeXml(var)%>
1184 </fun:Record>
1185 </fun:OutputVariable>
1186 >>
1187 case VARIABLE(__) then
1188 <<
1189 <fun:OutputVariable type="<%varTypeXml(var)%>">
1190 <fun:Name>
1191 <exp:QualifiedNamePart name="<%contextCrefXml(name,contextFunction)%>"/>
1192 </fun:Name>
1193 </fun:OutputVariable>
1194 >>
1195 case FUNCTION_PTR(__) then '<%name%>'
1196 end funOutputVariableXml;
1197
1198 3 template funArgDefinitionXml(Variable var)
1199 ::=
1200 match var
1201 case VARIABLE(ty=T_COMPLEX(complexClassType=RECORD(__))) then
1202 <<
1203 <fun:InputVariable type="Record">
1204 <fun:Name>
1205 <exp:QualifiedNamePart name="<%contextCrefXml(name,contextFunction)%>"/>
1206 </fun:Name>
1207 <fun:Record>
1208 <%varTypeXml(var)%>
1209 </fun:Record>
1210 </fun:InputVariable>
1211 >>
1212 case VARIABLE(__) then
1213 <<
1214 <fun:InputVariable type="<%varTypeXml(var)%>">
1215 <fun:Name>
1216 <exp:QualifiedNamePart name="<%contextCrefXml(name,contextFunction)%>"/>
1217 </fun:Name>
1218 <%/*underscorePathXml(ClassInfUtil.getStateName(complexClassType))*/%>
1219 </fun:InputVariable>
1220 >>
1221 case FUNCTION_PTR(__) then 'modelica_fnptr <%name%>'
1222 end funArgDefinitionXml;
1223
1224 ✗ template funVarDeclarationsXml(Variable var)
1225 ::=
1226 match var
1227 case VARIABLE(__) then
1228 <<
1229 <fun:protectedVariable type="<%varTypeXml(var)%>">
1230 <fun:Name>
1231 <exp:QualifiedNamePart name="<%contextCrefXml(name,contextFunction)%>"/>
1232 </fun:Name>
1233 <%/*underscorePathXml(ClassInfUtil.getStateName(complexClassType))*/%>
1234 </fun:ProtectedVariable>
1235 >>
1236 case FUNCTION_PTR(__) then 'modelica_fnptr <%name%>'
1237 end funVarDeclarationsXml;
1238
1239 ✗ template extFunctionNameXml(String name, String language)
1240 ::=
1241 match language
1242 case "C" then
1243 <<
1244 <exp:QualifiedName name="<%name%>"/>
1245 >>
1246 case "FORTRAN 77" then
1247 <<
1248 <exp:QualifiedName name="<%name%>"/>
1249 >>
1250 else error(sourceInfo(), 'Unsupport external language: <%language%>')
1251 end extFunctionNameXml;
1252
1253 ✗ template extTypeXml(Type type, Boolean isInput, Boolean isArray)
1254 "Generates type for external function argument or return value."
1255 ::=
1256 let s = match type
1257 case T_INTEGER(__) then "int"
1258 case T_REAL(__) then "double"
1259 case T_STRING(__) then "const char*"
1260 case T_BOOL(__) then "int"
1261 case T_ENUMERATION(__) then "int"
1262 case T_ARRAY(__) then extTypeXml(ty,isInput,true)
1263 case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__))
1264 then "void *"
1265 case T_COMPLEX(complexClassType=RECORD(path=rname))
1266 then 'struct <%underscorePathXml(rname)%>'
1267 case T_METATYPE(__) case T_METABOXED(__) then "modelica_metatype"
1268 else error(sourceInfo(), 'Unknown external C type <%unparseType(type)%>')
1269 match type case T_ARRAY(__) then s else if isInput then (if isArray then '<%match s case "const char*" then "" else "const "%><%s%>*' else s) else '<%s%>*'
1270 end extTypeXml;
1271
1272 ✗ template extTypeF77Xml(Type type, Boolean isReference)
1273 "Generates type for external function argument or return value for F77."
1274 ::=
1275 let s = match type
1276 case T_INTEGER(__) then "int"
1277 case T_REAL(__) then "double"
1278 case T_STRING(__) then "char"
1279 case T_BOOL(__) then "int"
1280 case T_ENUMERATION(__) then "int"
1281 case T_ARRAY(__) then extTypeF77Xml(ty, true)
1282 case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__))
1283 then "void*"
1284 case T_COMPLEX(complexClassType=RECORD(path=rname))
1285 then 'struct <%underscorePathXml(rname)%>'
1286 case T_METATYPE(__) case T_METABOXED(__) then "void*"
1287 else error(sourceInfo(), 'Unknown external F77 type <%unparseType(type)%>')
1288 match type case T_ARRAY(__) then s else if isReference then '<%s%>*' else s
1289 end extTypeF77Xml;
1290
1291 ✗ template functionNameXml(SimCodeFunction.Function fn, Boolean dotPath)
1292 ::=
1293 match fn
1294 case FUNCTION(__)
1295 case EXTERNAL_FUNCTION(__)
1296 case RECORD_CONSTRUCTOR(__) then if dotPath then dotPathXml(name) else underscorePathXml(name)
1297 end functionNameXml;
1298
1299 4 template extVarNameXml(ComponentRef cr)
1300 ::=
1301 <<
1302 <%crefXml(cr)%>
1303 >>
1304 end extVarNameXml;
1305
1306 1 template extFunCallXml(SimCodeFunction.Function fun, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
1307 "Generates the call to an external function."
1308 ::=
1309 match fun
1310 case EXTERNAL_FUNCTION(__) then
1311 match language
1312 case "C" then extFunCallCXml(fun, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1313 case "FORTRAN 77" then extFunCallF77Xml(fun, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1314 end extFunCallXml;
1315
1316 1 template extFunCallCXml(SimCodeFunction.Function fun, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
1317 "Generates the call to an external C function."
1318 ::=
1319 match fun
1320 case EXTERNAL_FUNCTION(__) then
1321 let args = (extArgs |> arg =>
1322 extArgCXml(arg, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1323 ;separator="\n ")
1324 let returnAssign = match extReturn case SIMEXTARG(cref=c) then
1325 '<%extVarNameXml(c)%> '
1326 else
1327 ""
1328 <<
1329 <fun:Assign>
1330 <%returnAssign%>
1331 <fun:Expression>
1332 <exp:FunctionCall>
1333 <exp:Name>
1334 <exp:QualifiedNamePart name="<%extName%>" />
1335 </exp:Name>
1336 <exp:Arguments>
1337 <%args%>
1338 </exp:Arguments>
1339 </exp:FunctionCall>
1340 </fun:Expression>
1341 </fun:Assign>
1342 >>
1343 end extFunCallCXml;
1344
1345 ✗ template extFunCallF77Xml(SimCodeFunction.Function fun, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
1346 "Generates the call to an external Fortran 77 function."
1347 ::=
1348 match fun
1349 case EXTERNAL_FUNCTION(__) then
1350 let args = (extArgs |> arg => extArgF77Xml(arg, &preExp, &varDecls) ;separator=", ")
1351 let returnAssign = match extReturn case SIMEXTARG(cref=c) then
1352 '<%extVarNameXml(c)%>'
1353 else
1354 ""
1355 <<
1356 <fun:Assign>
1357 <%returnAssign%>
1358 <fun:Expression>
1359 <exp:FunctionCall>
1360 <exp:Name>
1361 <exp:QualifiedNamePart name="<%extName%>" />
1362 </exp:Name>
1363 <exp:Arguments>
1364 <%args%>
1365 </exp:Arguments>
1366 </exp:FunctionCall>
1367 </fun:Expression>
1368 </fun:Assign>
1369 >>
1370 end extFunCallF77Xml;
1371
1372 3 template extArgCXml(SimExtArg extArg, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
1373 "Helper to extFunCallXml."
1374 ::=
1375 match extArg
1376 case SIMEXTARG(cref=c, outputIndex=oi, isArray=true, type_=t) then
1377 <<
1378 <%extVarNameXml(c)%>
1379 >>
1380 case SIMEXTARG(cref=c, isInput=ii, outputIndex=0, type_=t) then
1381 <<
1382 <%extVarNameXml(c)%>
1383 >>
1384 case SIMEXTARG(cref=c, isInput=ii, outputIndex=oi, type_=t) then
1385 <<
1386 <%extVarNameXml(c)%>
1387 >>
1388 case SIMEXTARGEXP(__) then
1389 daeExternalXmlExp(exp, contextFunction, &preExp /*BUFC*/, &varDecls /*BUFD*/) +'test daeexternal xml'
1390 case SIMEXTARGSIZE(cref=c) then
1391 let name = extVarNameXml(c)
1392 let dim = daeExpXml(exp, contextFunction, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1393 <<
1394 <exp:Size>
1395 <%name%>
1396 <%dim%>
1397 </exp:Size>
1398 >>
1399 end extArgCXml;
1400
1401 ✗ template extArgF77Xml(SimExtArg extArg, Text &preExp, Text &varDecls)
1402 ::=
1403 match extArg
1404 case SIMEXTARG(cref=c, isArray=true, type_=t) then
1405 <<
1406 <%extVarNameXml(c)%>
1407 >>
1408 case SIMEXTARG(cref=c, outputIndex=oi, type_=T_INTEGER(__)) then
1409 <<
1410 <%extVarNameXml(c)%>
1411 >>
1412 case SIMEXTARG(cref=c, outputIndex=oi, type_ = T_STRING(__)) then
1413 <<
1414 <%extVarNameXml(c)%>
1415 >>
1416 case SIMEXTARG(cref=c, outputIndex=oi, type_=t) then
1417 <<
1418 <%extVarNameXml(c)%>
1419 >>
1420 case SIMEXTARGEXP(exp=exp, type_ = T_STRING(__)) then
1421 let texp = daeExpXml(exp, contextFunction, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1422 <<
1423 <%texp%>
1424 >>
1425 case SIMEXTARGSIZE(cref=c) then
1426 let dim = daeExpXml(exp, contextFunction, &preExp, &varDecls)
1427 let name = extVarNameXml(c)
1428 <<
1429 <exp:Size>
1430 <%name%>
1431 <%dim%>
1432 </exp:Size>
1433 >>
1434 end extArgF77Xml;
1435
1436
1437 /*****************************************************************************
1438 * SECTION: GENERATE OPTIMIZATION IN SIMULATION FILE
1439 *****************************************************************************/
1440
1441 4 template objectiveFunctionXml( list<DAE.ClassAttributes> classAttributes ,SimCode simCode)
1442 "Generates XML for Objective Functions."
1443 ::=
1444 (classAttributes |> classAttribute => classAttributesXml(classAttribute,simCode); separator="\n")
1445
1446 end objectiveFunctionXml;
1447
1448 ✗ template classAttributesXml(ClassAttributes classAttribute, SimCode simCode)
1449 "Generates XML for class attributes of objective function."
1450 ::=
1451 match classAttribute
1452 case OPTIMIZATION_ATTRS(__) then
1453 let &varDecls = buffer "" /*BUFD*/
1454 let &preExp = buffer "" /*BUFD*/
1455 // let test = match objetiveE case SOME(exp) then
1456 // <<
1457 // <%daeExpXml(exp, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>
1458 // >>
1459 // else 'No cref for Objective '
1460 let objectiveFunction = match objetiveE case SOME(exp) then
1461 <<
1462 <opt:ObjectiveFunction>
1463 <%daeExpXml(exp, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>
1464 </opt:ObjectiveFunction>
1465 >>
1466 let objectiveIntegrand = match objectiveIntegrandE case SOME(exp) then
1467 <<
1468 <opt:IntegrandObjectiveFunction>
1469 <%daeExpXml(exp, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>
1470 </opt:IntegrandObjectiveFunction>
1471 >>
1472 let startTime = match startTimeE case SOME(exp) then
1473 <<
1474 <opt:IntervalStartTime>
1475 <opt:Value><%daeExpValueXml(exp, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%></opt:Value>
1476 </opt:IntervalStartTime>
1477 >>
1478 let finalTime = match finalTimeE case SOME(exp) then
1479 <<
1480 <opt:IntervalFinalTime>
1481 <opt:Value><%daeExpValueXml(exp, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%></opt:Value>
1482 </opt:IntervalFinalTime>
1483 >>
1484 let timePointIndex = match startTimeE case SOME(exp) then
1485 <<
1486 index = "<%daeExpValueXml(exp, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>"
1487 >>
1488 let timePointValue = match finalTimeE case SOME(exp) then
1489 <<
1490 value = "<%daeExpValueXml(exp, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>"
1491 >>
1492 let constraints = match simCode case SIMCODE(modelInfo = MODELINFO(__)) then constraintsXml(constraints)
1493 <<
1494 <opt:Optimization>
1495 <%objectiveFunction%>
1496 <%objectiveIntegrand%>
1497 <%startTime%>
1498 <%finalTime%>
1499 <opt:TimePoints>
1500 <opt:TimePoint <%timePointIndex%> <%timePointValue%>>
1501 <%/*test*/%>
1502 </opt:TimePoint>
1503 </opt:TimePoints>
1504 <opt:PathConstraints>
1505 <%constraints%>
1506 </opt:PathConstraints>
1507 </opt:Optimization>
1508 >>
1509 else error(sourceInfo(), 'Unknown Optimization attribute')
1510 end classAttributesXml;
1511
1512 ✗ template constraintsXml( list<DAE.Constraint> constraints)
1513 "Generates XML for Optimization."
1514 ::=
1515 (constraints |> constraint => constraintXml(constraint); separator="\n")
1516
1517 end constraintsXml;
1518
1519 ✗ template constraintXml(Constraint cons)
1520 "Generates XML for List of Constraints."
1521 ::=
1522 match cons
1523 case CONSTRAINT_EXPS(__) then
1524 let &varDecls = buffer "" /*BUFD*/
1525 let &preExp = buffer "" /*BUFD*/
1526 let constrain = (constraintLst |> constraint =>
1527 daeExpConstraintXml(constraint, contextSimulationDiscrete, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1528 ;separator="\n")
1529 <<
1530 <%constrain%>
1531 >>
1532 else error(sourceInfo(), 'Unknown Constraint List')
1533 end constraintXml;
1534
1535 /*****************************************************************************
1536 * SECTION: GENERATE All Algorithm IN SIMULATION FILE
1537 *****************************************************************************/
1538
1539 ✗ template funStatementXml(list<DAE.Statement> statementLst, Text &varDecls /*BUFP*/)
1540 "Generates function statements."
1541 ::=
1542 statementLst |> stmt => algStatementXml(stmt, contextFunction, &varDecls /*BUFD*/) ;separator="\n"
1543 end funStatementXml;
1544
1545 2 template algStatementXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1546 "Generates an algorithm statement."
1547 ::=
1548 let res = match stmt
1549 case s as STMT_ASSIGN(__) then algStmtAssignXml(s, context, &varDecls /*BUFD*/)
1550 case s as STMT_ASSIGN_ARR(__) then algStmtAssignArrXml(s, context, &varDecls /*BUFD*/)
1551 case s as STMT_TUPLE_ASSIGN(__) then algStmtTupleAssignXml(s, context, &varDecls /*BUFD*/)
1552 case s as STMT_IF(__) then algStmtIfXml(s, context, &varDecls /*BUFD*/)
1553 case s as STMT_FOR(__) then algStmtForXml(s, context, &varDecls /*BUFD*/)
1554 case s as STMT_WHILE(__) then algStmtWhileXml(s, context, &varDecls /*BUFD*/)
1555 case s as STMT_ASSERT(__) then algStmtAssertXml(s, context, &varDecls /*BUFD*/)
1556 case s as STMT_TERMINATE(__) then algStmtTerminateXml(s, context, &varDecls /*BUFD*/)
1557 case s as STMT_WHEN(__) then algStmtWhenXml(s, context, &varDecls /*BUFD*/)
1558 case s as STMT_BREAK(__) then '<fun:Break/><%\n%>'
1559 case s as STMT_RETURN(__) then '<fun:Return/><%\n%>'
1560 case s as STMT_NORETCALL(__) then algStmtNoretcallXml(s, context, &varDecls /*BUFD*/)
1561 case s as STMT_REINIT(__) then algStmtReinitXml(s, context, &varDecls /*BUFD*/)
1562 else error(sourceInfo(), 'ALG_STATEMENT NYI')
1563 <<
1564 <%res%>
1565 >>
1566 end algStatementXml;
1567
1568 ✗ template algStmtAssignXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1569 "Generates an assigment algorithm statement."
1570 ::=
1571 match stmt
1572 case STMT_ASSIGN(exp1=CREF(componentRef=WILD(__)), exp=e) then
1573 let &preExp = buffer "" /*BUFD*/
1574 let expPart = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1575 <<
1576 <fun:Assign>
1577 <fun:Expression>
1578 <%expPart%>
1579 </fun:Expression>
1580 </fun:Assign>
1581 >>
1582 case STMT_ASSIGN(exp1=CREF(ty = T_FUNCTION_REFERENCE_VAR(__)))
1583 case STMT_ASSIGN(exp1=CREF(ty = T_FUNCTION_REFERENCE_FUNC(__))) then
1584 let &preExp = buffer "" /*BUFD*/
1585 let varPart = scalarLhsCrefXml(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1586 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1587 <<
1588 <fun:Assign>
1589 <%varPart%>
1590 <fun:Expression>
1591 <%expPart%>
1592 </fun:Expression>
1593 </fun:Assign>
1594 >>
1595 case STMT_ASSIGN(exp1=CREF(__)) then
1596 let &preExp = buffer "" /*BUFD*/
1597 let varPart = scalarLhsCrefXml(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1598 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1599 <<
1600 <fun:Assign>
1601 <%varPart%>
1602 <fun:Expression>
1603 <%expPart%>
1604 </fun:Expression>
1605 </fun:Assign>
1606 >>
1607 case STMT_ASSIGN(exp1=exp1 as ASUB(__),exp=val) then
1608 (match expTypeFromExpShortXml(exp)
1609 case "metatype" then
1610 // MetaModelica Array
1611 (match exp case ASUB(exp=arr, sub={idx}) then
1612 let &preExp = buffer ""
1613 let arr1 = daeExpXml(arr, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1614 let idx1 = daeSubscriptXML(idx, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1615 let val1 = daeExpXml(val, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1616 <<
1617 <%preExp%>
1618 <%arr1%>
1619 <%idx1%>
1620 <%val1%>
1621 >>)
1622 // Modelica Array
1623 else
1624 let &preExp = buffer "" /*BUFD*/
1625 let varPart = daeExpAsubXml(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1626 let expPart = daeExpXml(val, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1627 <<
1628 <fun:Assign>
1629 <%varPart%>
1630 <fun:Expression>
1631 <%expPart%>
1632 </fun:Expression>
1633 </fun:Assign>
1634 >>
1635 )
1636 case STMT_ASSIGN(__) then
1637 let &preExp = buffer "" /*BUFD*/
1638 let expPart1 = daeExpXml(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1639 let expPart2 = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1640 <<
1641 <fun:Assign>
1642 <%expPart1%>
1643 <fun:Expression>
1644 <%expPart2%>
1645 </fun:Expression>
1646 </fun:Assign>
1647 >>
1648 end algStmtAssignXml;
1649
1650 ✗ template algStmtAssignArrXml(DAE.Statement stmt, Context context,
1651 Text &varDecls /*BUFP*/)
1652 "Generates an array assigment algorithm statement."
1653 ::=
1654 match stmt
1655 case STMT_ASSIGN_ARR(exp=e, lhs=CREF(componentRef=cr), type_=t) then
1656 let &preExp = buffer "" /*BUFD*/
1657 let expPart = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1658 let ispec = indexSpecFromCrefXml(cr, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1659 if ispec then
1660 <<
1661 <%preExp%>
1662 <%indexedAssignXml(t, expPart, cr, ispec, context, &varDecls)%>
1663 >>
1664 else
1665 <<
1666 <fun:Assign>
1667 <%copyArrayDataXml(t, expPart, cr, context)%>
1668 <fun:Expression>
1669 <%preExp%>
1670 </fun:Expression>
1671 </fun:Assign>
1672 >>
1673 end algStmtAssignArrXml;
1674
1675 ✗ template indexedAssignXml(DAE.Type ty, String exp, DAE.ComponentRef cr,
1676 String ispec, Context context, Text &varDecls)
1677 ::=
1678 let type = expTypeArrayXml(ty)
1679 let cref = contextArrayCrefXml(cr, context)
1680 match context
1681 case FUNCTION_CONTEXT(__) then
1682 <<
1683 <%cref%>
1684 >>
1685 else
1686 <<
1687 <%exp%>
1688 <%ispec%>
1689 <%cref%>
1690 >>
1691 end indexedAssignXml;
1692
1693 ✗ template copyArrayDataXml(DAE.Type ty, String exp, DAE.ComponentRef cr,
1694
1695 Context context)
1696 ::=
1697 let type = expTypeArrayXml(ty)
1698 let cref = contextArrayCrefXml(cr, context)
1699 match context
1700 case FUNCTION_CONTEXT(__) then '<%cref%>'
1701 else
1702 <<
1703 <%cref%>
1704 >>
1705
1706 end copyArrayDataXml;
1707
1708 ✗ template algStmtTupleAssignXml(DAE.Statement stmt, Context context,
1709 Text &varDecls /*BUFP*/)
1710 "Generates XML for a tuple assigment algorithm statement."
1711 ::=
1712 match stmt
1713 case STMT_TUPLE_ASSIGN(exp=CALL(__)) then
1714 let &preExp = buffer "" /*BUFD*/
1715 let &afterExp = buffer "" /*BUFD*/
1716 let crefs = (expExpLst |> e => ExpressionDumpTpl.dumpExp(e,"\"") ;separator=", ")
1717 let marker = '(<%crefs%>) = <%ExpressionDumpTpl.dumpExp(exp,"\"")%>'
1718 let &preExp += '/* algStmtTupleAssign: preExp buffer created for <%marker%> */<%\n%>'
1719 let &afterExp += '/* algStmtTupleAssign: afterExp buffer created for <%marker%> */<%\n%>'
1720 let retStruct = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1721 let lhsCrefs = (expExpLst |> cr hasindex i1 fromindex 1 =>
1722 let rhsStr = '<%retStruct%>.targ<%i1%>'
1723 writeLhsCrefXml(cr, rhsStr, context, &afterExp /*BUFC*/, &varDecls /*BUFD*/)
1724 ;separator="\n")
1725 <<
1726 <fun:FunctionCallStatement>
1727 <fun:OutputArgument>
1728 <%lhsCrefs%>
1729 </fun:OutputArgument>
1730 <%retStruct%>
1731 </fun:FunctionCallStatement>
1732 >>
1733 case STMT_TUPLE_ASSIGN(exp=MATCHEXPRESSION(__)) then
1734 let &preExp = buffer "" /*BUFD*/
1735 let &afterExp = buffer "" /*BUFD*/
1736 let prefix = 'tmp<%System.tmpTick()%>'
1737 //let _ = daeExpMatch2Xml(exp, expExpLst, prefix, context, &preExp, &varDecls)
1738 let lhsCrefs = (expExpLst |> cr hasindex i1 fromindex 1 =>
1739 let rhsStr = '<%prefix%>_targ<%i1%>'
1740 writeLhsCrefXml(cr, rhsStr, context, &afterExp /*BUFC*/, &varDecls /*BUFD*/)
1741 ;separator="\n")
1742 <<
1743 <%expExpLst |> cr hasindex i1 fromindex 1 =>
1744 let rhsStr = '<%prefix%>_targ<%i1%>'
1745 let typ = '<%expTypeFromExpModelicaXml(cr)%>'
1746 let initVar = match typ case "modelica_metatype" then ' = NULL' else ''
1747 let addRoot = match typ case "modelica_metatype" then ' mmc_GC_add_root(&<%rhsStr%>, mmc_GC_local_state, "<%rhsStr%>");' else ''
1748 let &varDecls += '<%typ%> <%rhsStr%><%initVar%>;<%addRoot%><%\n%>'
1749 ""
1750 ;separator="\n";empty%>
1751 <%preExp%>
1752 <%lhsCrefs%>
1753 <%afterExp%>
1754 >>
1755 else error(sourceInfo(), 'algStmtTupleAssign failed')
1756 end algStmtTupleAssignXml;
1757
1758 ✗ template writeLhsCrefXml(Exp exp, String rhsStr, Context context, Text &preExp /*BUFP*/,
1759 Text &varDecls /*BUFP*/)
1760 "Generates XML code for writing a returnStructur to var."
1761 ::=
1762 match exp
1763 case ecr as CREF(componentRef=WILD(__)) then
1764 <<
1765 <fun:EmptyOutputArgument></fun:EmptyOutputArgument>
1766 >>
1767 case CREF(ty= t as DAE.T_ARRAY(__)) then
1768 let lhsStr = scalarLhsCrefXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1769 match context
1770 case SIMULATION_CONTEXT(__) then
1771 <<
1772 <%lhsStr%>
1773 >>
1774 else
1775 <<
1776 <%lhsStr%>
1777 >>
1778
1779 case UNARY(exp = e as CREF(ty= t as DAE.T_ARRAY(__))) then
1780 let lhsStr = scalarLhsCrefXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1781 match context
1782 case SIMULATION_CONTEXT(__) then
1783 <<
1784 <%rhsStr%>
1785 <%lhsStr%>
1786 >>
1787 else
1788 <<
1789 <%lhsStr%>
1790 >>
1791 case CREF(__) then
1792 let lhsStr = scalarLhsCrefXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1793 <<
1794 <%lhsStr%>
1795 >>
1796 case UNARY(exp = e as CREF(__)) then
1797 let lhsStr = scalarLhsCrefXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1798 <<
1799 <%lhsStr%>
1800 >>
1801 case _ then
1802 <<
1803 /* SimCodeC.tpl template: writeLhsCref: UNHANDLED LHS
1804 * <%ExpressionDumpTpl.dumpExp(exp,"\"")%> = <%rhsStr%>
1805 */
1806 >>
1807 end writeLhsCrefXml;
1808
1809 ✗ template algStmtIfXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1810 "Generates an if algorithm statement."
1811 ::=
1812 match stmt
1813 case STMT_IF(__) then
1814 let &preExp = buffer "" /*BUFD*/
1815 let condExp = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1816 <<
1817 <fun:If>
1818 <fun:Condition>
1819 <%condExp%>
1820 </fun:Condition>
1821 <fun:Statements>
1822 <%statementLst |> stmt => algStatementXml(stmt, context, &varDecls /*BUFD*/) ;separator="\n"%>
1823 </fun:Statements>
1824 <%elseExprXml(else_, context, &varDecls /*BUFD*/)%>
1825 </fun:If>
1826 >>
1827 end algStmtIfXml;
1828
1829 ✗ template algStmtForXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1830 "Generates a for algorithm statement."
1831 ::=
1832 match stmt
1833 case s as STMT_FOR(range=rng as RANGE(__)) then
1834 algStmtForRangeXml(s, context, &varDecls /*BUFD*/)
1835 case s as STMT_FOR(__) then
1836 algStmtForGenericXml(s, context, &varDecls /*BUFD*/)
1837 end algStmtForXml;
1838
1839 ✗ template algStmtForRangeXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1840 "Generates a for algorithm statement where range is RANGE."
1841 ::=
1842 match stmt
1843 case STMT_FOR(range=rng as RANGE(__)) then
1844 let identType = expTypeXml(type_, iterIsArray)
1845 let identTypeShort = expTypeShortXml(type_)
1846 let stmtStr = (statementLst |> stmt => algStatementXml(stmt, context, &varDecls)
1847 ;separator="\n")
1848 algStmtForRange_implXml(rng, iter, identType, identTypeShort, stmtStr, context, &varDecls)
1849 end algStmtForRangeXml;
1850
1851 ✗ template algStmtForRange_implXml(Exp range, Ident iterator, String type, String shortType, Text body, Context context, Text &varDecls)
1852 "The implementation of algStmtForRange, ."
1853 ::=
1854 match range
1855 case RANGE(__) then
1856 let iterName = contextIteratorNameXml(iterator, context)
1857 let &preExp = buffer ""
1858 let startValue = daeExpXml(start, context, &preExp, &varDecls)
1859 let stepValue = match step case SOME(eo) then
1860 daeExpXml(eo, context, &preExp, &varDecls)
1861 else
1862 '' //because the default step value is 1
1863 let stopValue = daeExpXml(stop, context, &preExp, &varDecls)
1864 <<
1865 <fun:For>
1866 <fun:Index>
1867 <fun:IterationVariable>
1868 <exp:QualifiedNamePart name="<%iterName%>"/>
1869 </fun:IterationVariable>
1870 <fun:IterationSet>
1871 <exp:Range>
1872 <%startValue%>
1873 <%stepValue%>
1874 <%stopValue%>
1875 </exp:Range>
1876 </fun:IterationSet>
1877 </fun:Index>
1878 <fun:Statements>
1879 <%body%>
1880 </fun:Statements>
1881 </fun:For>
1882 >>
1883 end algStmtForRange_implXml;
1884
1885 ✗ template algStmtForGenericXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1886 "Generates a for algorithm statement where range is not RANGE."
1887 ::=
1888 match stmt
1889 case STMT_FOR(__) then
1890 let iterType = expTypeXml(type_, iterIsArray)
1891 let arrayType = expTypeArrayXml(type_)
1892
1893 let stmtStr = (statementLst |> stmt =>
1894 algStatementXml(stmt, context, &varDecls) ;separator="\n")
1895 algStmtForGeneric_implXml(range, iter, iterType, arrayType, iterIsArray, stmtStr,
1896 context, &varDecls)
1897 end algStmtForGenericXml;
1898
1899 ✗ template algStmtForGeneric_implXml(Exp exp, Ident iterator, String type,
1900 String arrayType, Boolean iterIsArray, Text &body, Context context, Text &varDecls)
1901 "The implementation of algStmtForGeneric, which is also used by daeExpReduction."
1902 ::=
1903 let iterName = contextIteratorNameXml(iterator, context)
1904 let &preExp = buffer ""
1905 let evar = daeExpXml(exp, context, &preExp, &varDecls)
1906 <<
1907 <fun:For>
1908 <fun:Index>
1909 <fun:IterationVariable>
1910 <exp:QualifiedNamePart name="<%iterName%>"/>
1911 </fun:IterationVariable>
1912 <fun:IterationSet>
1913 <exp:Array>
1914 <%preExp%>
1915 </exp:Array>
1916 </fun:IterationSet>
1917 </fun:Index>
1918 <fun:Statements>
1919 <%body%>
1920 </fun:Statements>
1921 </fun:For>
1922 >>
1923 end algStmtForGeneric_implXml;
1924
1925 ✗ template algStmtWhileXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1926 "Generates a while algorithm statement."
1927 ::=
1928 match stmt
1929 case STMT_WHILE(__) then
1930 let &preExp = buffer "" /*BUFD*/
1931 let var = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1932 <<
1933 <fun:While>
1934 <fun:Condition>
1935 <%var%>
1936 </fun:Condition>
1937 <fun:Statements>
1938 <%statementLst |> stmt => algStatementXml(stmt, context, &varDecls /*BUFD*/) ;separator="\n"%>
1939 </fun:Statements>
1940 </fun:While>
1941 >>
1942 end algStmtWhileXml;
1943
1944 2 template algStmtAssertXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1945 "Generates an assert algorithm statement."
1946 ::=
1947 match stmt
1948 case STMT_ASSERT(source=SOURCE(info=info)) then
1949 assertCommonXml(cond, msg, context, &varDecls, info)
1950 end algStmtAssertXml;
1951
1952 ✗ template algStmtTerminateXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1953 "Generates an assert algorithm statement."
1954 ::=
1955 match stmt
1956 case STMT_TERMINATE(__) then
1957 let &preExp = buffer "" /*BUFD*/
1958 let msgVar = daeExpXml(msg, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1959 <<
1960 <%preExp%>
1961 <%msgVar%>
1962 >>
1963 end algStmtTerminateXml;
1964
1965 ✗ template algStmtNoretcallXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
1966 "Generates a no return call algorithm statement."
1967 ::=
1968 match stmt
1969 case STMT_NORETCALL(__) then
1970 let &preExp = buffer "" /*BUFD*/
1971 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
1972 <<
1973 <%preExp%>
1974 <%expPart%>
1975 >>
1976
1977 end algStmtNoretcallXml;
1978
1979 ✗ template algStmtWhenXml(DAE.Statement when, Context context, Text &varDecls /*BUFP*/)
1980 "Generates a when algorithm statement."
1981 ::=
1982 match when
1983 case STMT_WHEN(__) then
1984 let cond = (conditions |> e => '<%crefToXmlStr(e)%>';separator="\n")
1985 let statements = (statementLst |> stmt =>
1986 algStatementXml(stmt, context, &varDecls /*BUFD*/)
1987 ;separator="\n")
1988 let else = algStatementWhenElseXml(elseWhen, &varDecls /*BUFD*/)
1989 <<
1990 <fun:When>
1991 <fun:Condition>
1992 <%cond%>
1993 </fun:Condition>
1994 <fun:Statements>
1995 <%statements%>
1996 </fun:Statements>
1997 <%else%>
1998 >>
1999 end match
2000 end algStmtWhenXml;
2001
2002 ✗ template algStatementWhenElseXml(Option<DAE.Statement> stmt, Text &varDecls /*BUFP*/)
2003 "Helper to algStmtWhen."
2004 ::=
2005 match stmt
2006 case SOME(when as STMT_WHEN(__)) then
2007 let statements = (when.statementLst |> stmt =>
2008 algStatementXml(stmt, contextSimulationDiscrete, &varDecls /*BUFD*/)
2009 ;separator="\n")
2010 let else = algStatementWhenElseXml(when.elseWhen, &varDecls /*BUFD*/)
2011 let elseCondStr = (when.conditions |> e => '<%crefToXmlStr(e)%>';separator="\n ")
2012 <<
2013 <fun:Condition>
2014 <%elseCondStr%>
2015 </fun:Condition>
2016 <fun:Statements>
2017 <%statements%>
2018 </fun:Statements>
2019 <%else%>
2020 </fun:When>
2021 >>
2022 end algStatementWhenElseXml;
2023
2024 ✗ template algStmtReinitXml(DAE.Statement stmt, Context context, Text &varDecls /*BUFP*/)
2025 "Generates an assigment algorithm statement."
2026 ::=
2027 match stmt
2028 case STMT_REINIT(__) then
2029 let &preExp = buffer "" /*BUFD*/
2030 let expPart1 = daeExpXml(var, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2031 let expPart2 = daeExpXml(value, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2032 <<
2033 <exp:Reinit>
2034 <%expPart1%>
2035 <%expPart2%>
2036 </exp:Reinit>
2037 >>
2038 end algStmtReinitXml;
2039
2040 ✗ template indexSpecFromCrefXml(ComponentRef cr, Context context, Text &preExp /*BUFP*/,
2041 Text &varDecls /*BUFP*/)
2042 "Helper to algStmtAssignArr.
2043 Currently works only for CREF_IDENT."
2044 ::=
2045 match cr
2046 case CREF_IDENT(subscriptLst=subs as (_ :: _)) then
2047 daeExpCrefRhsIndexSpecXml(subs, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2048 end indexSpecFromCrefXml;
2049
2050 ✗ template elseExprXml(DAE.Else else_, Context context, Text &varDecls /*BUFP*/)
2051 "Helper to algStmtIf."
2052 ::=
2053 match else_
2054 case NOELSE(__) then
2055 ""
2056 case ELSEIF(__) then
2057 let &preExp = buffer "" /*BUFD*/
2058 let condExp = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2059 <<
2060 <fun:ElseIf>
2061 <fun:Condition>
2062 <%condExp%>
2063 </fun:Condition>
2064 <%statementLst |> stmt =>algStatementXml(stmt, context, &varDecls /*BUFD*/);separator="\n"%>
2065 </fun:ElseIf>
2066 <%elseExprXml(else_, context, &varDecls /*BUFD*/)%>
2067 >>
2068 case ELSE(__) then
2069 <<
2070 <fun:Else>
2071 <%statementLst |> stmt =>
2072 algStatementXml(stmt, context, &varDecls /*BUFD*/)
2073 ;separator="\n"%>
2074 </fun:Else>
2075 >>
2076 end elseExprXml;
2077
2078 ✗ template scalarLhsCrefXml(Exp ecr, Context context, Text &preExp, Text &varDecls)
2079 "Generates the left hand side (for use on left hand side) of a component
2080 reference."
2081 ::=
2082 match ecr
2083 case CREF(componentRef = cr, ty = T_FUNCTION_REFERENCE_VAR(__)) then
2084 <<
2085 <%crefStrXml(cr)%>
2086 >>
2087 case ecr as CREF(componentRef=CREF_IDENT(__)) then
2088 if crefNoSub(ecr.componentRef) then
2089 crefXml(ecr.componentRef)
2090 else
2091 daeExpCrefLhsXml(ecr, context, &preExp, &varDecls)
2092 case ecr as CREF(componentRef=CREF_QUAL(__)) then
2093 crefXml(ecr.componentRef)
2094 case ecr as CREF(componentRef=WILD(__)) then
2095 ''
2096 else
2097 "ONLY_IDENT_OR_QUAL_CREF_SUPPORTED_SLHS"
2098 end scalarLhsCrefXml;
2099
2100 /*****************************************************************************
2101 * SECTION: GENERATE All DAE Expression IN SIMULATION FILE
2102 *****************************************************************************/
2103
2104 488 template daeExpXml(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
2105 "Root Template for Expression-XML generation."
2106 ::=
2107 let e = daeExpXml_dispatch(exp, context, &preExp /*BUFP*/, &varDecls /*BUFP*/)
2108 let eStr1 = if e then e else preExp
2109 let eStr2 = if intEq(0, stringFind(eStr1, "tmp")) then preExp else eStr1
2110 eStr2
2111 end daeExpXml;
2112
2113 202 template daeExpXml_dispatch(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
2114 "Root Template for Expression-XML generation."
2115 ::=
2116 match exp
2117 case e as ICONST(__) then '<exp:IntegerLiteral><%integer%></exp:IntegerLiteral>'
2118 case e as RCONST(__) then '<exp:RealLiteral><%real%></exp:RealLiteral>'
2119 case e as SCONST(__) then '<exp:StringLiteral><%daeExpSconstXml(string, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)%></exp:StringLiteral>'
2120 case e as BCONST(__) then '<exp:BooleanLiteral>' + (if bool then "1" else "0") + '</exp:BooleanLiteral>'
2121 case e as ENUM_LITERAL(__) then index
2122 case e as CREF(__) then daeExpCrefRhsXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2123 case e as BINARY(__) then daeExpBinaryXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2124 case e as UNARY(__) then daeExpUnaryXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2125 case e as LBINARY(__) then daeExpLbinaryXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2126 case e as LUNARY(__) then daeExpLunaryXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2127 case e as RELATION(__) then daeExpRelationXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2128 case e as IFEXP(__) then daeExpIfXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2129 case e as CALL(__) then daeExpCallXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2130 case e as ARRAY(__) then daeExpArrayXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2131 case e as MATRIX(__) then daeExpMatrixXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2132 case e as RANGE(__) then daeExpRangeXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2133 case e as CAST(__) then daeExpCastXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2134 case e as ASUB(__) then daeExpAsubXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2135 case e as TSUB(__) then '<%daeExpXml(exp, context, &preExp, &varDecls)%>'
2136 case e as SIZE(__) then daeExpSizeXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2137 case e as TUPLE(__) then 'Tuple Not yet Implemented'
2138 case e as BOX(__) then daeExpBoxXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2139 case e as UNBOX(__) then daeExpUnboxXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2140 case e as SHARED_LITERAL(__) then daeExpSharedLiteralXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2141 else error(sourceInfo(), 'Unknown expression: <%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
2142 end daeExpXml_dispatch;
2143
2144 ✗ template daeExpValueXml(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
2145 "Expression-XML generation mainly used for optimica extension start and final value."
2146 ::=
2147 match exp
2148 case e as ICONST(__) then '<%integer%>'
2149 case e as RCONST(__) then '<%real%>'
2150 end daeExpValueXml;
2151
2152 ✗ template daeExternalXmlExp(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
2153 "Like daeExp, "
2154 ::=
2155 match typeof(exp)
2156 case T_ARRAY(__) then // Array-expressions
2157 <<
2158 <%daeExpXml(exp, context, &preExp, &varDecls)%>
2159 >>
2160 end daeExternalXmlExp;
2161
2162 5 template daeExpSconstXml(String string, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
2163 "Generates code for a string constant."
2164 ::=
2165 <<
2166 "<%Util.escapeModelicaStringToXmlString(string)%>"
2167 >>
2168 end daeExpSconstXml;
2169
2170
2171 /*********************************************************************
2172 *********************************************************************
2173 * RIGHT HAND SIDE
2174 *********************************************************************
2175 *********************************************************************/
2176
2177 202 template daeExpCrefRhsXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2178 Text &varDecls /*BUFP*/)
2179 "Generates code for a component reference on the right hand side of an
2180 expression."
2181 ::=
2182 match exp
2183 // A record cref without subscripts (i.e. a record instance) is handled
2184 // by daeExpRecordCrefRhs only in a simulation context, not in a function.
2185 case CREF(componentRef = cr, ty = t as T_COMPLEX(complexClassType = RECORD(path = _))) then
2186 match context case FUNCTION_CONTEXT(__) then
2187 daeExpCrefRhs2Xml(exp, context, &preExp, &varDecls)
2188 else
2189 daeExpRecordCrefRhsXml(t, cr, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2190 case CREF(componentRef = cr, ty = T_FUNCTION_REFERENCE_FUNC(__)) then
2191 <<
2192 <%crefFunctionNameXml(cr)%>
2193 >>
2194
2195 case CREF(componentRef = cr, ty = T_FUNCTION_REFERENCE_VAR(__)) then
2196 <<
2197 <%crefStrXml(cr)%>
2198 >>
2199 else daeExpCrefRhs2Xml(exp, context, &preExp, &varDecls)
2200 end daeExpCrefRhsXml;
2201
2202 202 template daeExpCrefRhs2Xml(Exp ecr, Context context, Text &preExp /*BUFP*/,
2203 Text &varDecls /*BUFP*/)
2204 "Generates code for a component reference."
2205 ::=
2206 match ecr
2207 case ecr as CREF(componentRef=cr, ty=ty) then
2208 // let &preExp += '/* daeExpCrefRhs2 begin preExp (<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) */<%\n%>'
2209 let box = daeExpCrefRhsArrayBoxXml(ecr, context, &preExp, &varDecls)
2210 if box then
2211 box
2212 else
2213 if crefIsScalar(cr, context)
2214 then
2215 <<
2216 <%crefXml(ecr.componentRef)%>
2217 >>
2218
2219 else
2220 if crefSubIsScalar(cr)
2221 then
2222 // The array subscript results in a scalar
2223 // let &preExp += '/* daeExpCrefRhs2 SCALAR(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) preExp */<%\n%>'
2224 let arrName = contextCrefXml(crefStripLastSubs(cr), context)
2225 let arrayType = expTypeArrayXml(ty)
2226 let dimsLenStr = listLength(crefSubs(cr))
2227 match arrayType
2228 case "metatype_array" then
2229 let dimsValuesStr = (crefSubs(cr) |> INDEX(__) =>
2230 daeExpXml(exp, context, &preExp, &varDecls)
2231 ;separator=", ")
2232 'arrayGet(<%arrName%>,<%dimsValuesStr%>) /* DAE.CREF */'
2233 else
2234 match context
2235 case FUNCTION_CONTEXT(__) then
2236 let dimsValuesStr = (crefSubs(cr) |> INDEX(__) =>
2237 daeExpXml(exp, context, &preExp, &varDecls)
2238 ;separator="\n ")
2239 <<
2240 <exp:Identifier>
2241 <exp:QualifiedNamePart name="<%arrName%>">
2242 <exp:ArraySubscripts>
2243 <exp:IndexExpression>
2244 <%dimsValuesStr%>
2245 </exp:IndexExpression>
2246 </exp:ArraySubscripts>
2247 </exp:QualifiedNamePart>
2248 </exp:Identifier>
2249 >>
2250 else
2251 match crefLastType(cr)
2252 case et as T_ARRAY(__) then
2253 <<
2254 (&<%arrName%>)[<%threadDimSubListXml(et.dims,crefSubs(cr),context,&preExp,&varDecls)%>]
2255 >>
2256 else error(sourceInfo(),'Indexing non-array <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>')
2257 else
2258 // The array subscript denotes a slice
2259 // let &preExp += '/* daeExpCrefRhs2 SLICE(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) preExp */<%\n%>'
2260 let arrName = contextArrayCrefXml(cr, context)
2261 let arrayType = expTypeArrayXml(ty)
2262 let tmp = tempDeclXml(arrayType, &varDecls /*BUFD*/)
2263 let spec1 = daeExpCrefRhsIndexSpecXml(crefSubs(cr), context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2264 let &preExp +=
2265 <<
2266 <%arrName%>
2267 <%spec1%><%\n%>
2268 >>
2269 tmp
2270
2271 case ecr then
2272 // let &preExp += '/* daeExpCrefRhs2 UNHANDLED(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) preExp */<%\n%>'
2273 error(sourceInfo(),'daeExpCrefRhs2: UNHANDLED EXPRESSION: <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>')
2274 end daeExpCrefRhs2Xml;
2275
2276 ✗ template threadDimSubListXml(list<Dimension> dims, list<DAE.Subscript> subs, Context context, Text &preExp, Text &varDecls)
2277 "Do direct indexing since sizes are known during compile-time"
2278 ::=
2279 match subs
2280 case {} then error(sourceInfo(),"Empty dimensions in indexing cref?")
2281 case (sub as INDEX(__))::subrest
2282 then
2283 match dims
2284 case _::dimrest
2285 then
2286 let estr = daeExpXml(sub.exp, context, &preExp, &varDecls)
2287 '((<%estr%>)<%
2288 dimrest |> dim =>
2289 match dim
2290 case DIM_INTEGER(__) then '*<%integer%>'
2291 case DIM_ENUM(__) then '*<%size%>'
2292 else error(sourceInfo(),"Non-constant dimension in simulation context")
2293 %>)<%match subrest case {} then "" else '+<%threadDimSubListXml(dimrest,subrest,context,&preExp,&varDecls)%>'%>'
2294 else error(sourceInfo(),"Less subscripts that dimensions in indexing cref? That's odd!")
2295 else error(sourceInfo(),"Non-index subscript in indexing cref? That's odd!")
2296 end threadDimSubListXml;
2297
2298 ✗ template daeExpCrefRhsIndexSpecXml(list<DAE.Subscript> subs, Context context,
2299 Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
2300 "Helper to daeExpCrefRhs."
2301 ::=
2302 let nridx_str = listLength(subs)
2303 let idx_str = (subs |> sub =>
2304 match sub
2305 case INDEX(__) then
2306 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2307 let str =
2308 <<
2309 <%expPart%>
2310 >>
2311 str
2312 case WHOLEDIM(__) then
2313 let str = <<(1), (int*)0, 'W'>>
2314 str
2315 case SLICE(__) then
2316 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2317 let tmp = tempDeclXml("modelica_integer", &varDecls /*BUFD*/)
2318 let &preExp += '<%tmp%> = size_of_dimension_integer_array(&<%expPart%>, 1);<%\n%>'
2319 let str = <<<%tmp%>, integer_array_make_index_array(<%expPart%>), 'A'>>
2320 str
2321 ;separator=", ")
2322 let tmp = tempDeclXml("index_spec_t", &varDecls /*BUFD*/)
2323 let &preExp += 'create_index_spec(&<%tmp%>, <%nridx_str%>, <%idx_str%>);<%\n%>'
2324 tmp
2325 end daeExpCrefRhsIndexSpecXml;
2326
2327 202 template daeExpCrefRhsArrayBoxXml(Exp ecr, Context context, Text &preExp /*BUFP*/,
2328 Text &varDecls /*BUFP*/)
2329 "Helper to daeExpCrefRhs."
2330 ::=
2331 match ecr
2332 case ecr as CREF(ty=T_ARRAY(ty=aty,dims=dims)) then
2333 match context
2334 case FUNCTION_CONTEXT(__) then ''
2335 else
2336 // For context simulation and other array variables must be boxed into a real_array
2337 // object since they are represented only in a double array.
2338 let tmpArr = tempDeclXml(expTypeArrayXml(aty), &varDecls /*BUFD*/)
2339 let dimsLenStr = listLength(dims)
2340 let dimsValuesStr = (dims |> dim => dimensionXml(dim) ;separator=", ")
2341 let type = expTypeShortXml(aty)
2342 let &preExp +=
2343 <<
2344 <%arrayCrefXmlStr(ecr.componentRef)%><%\n%>
2345 >>
2346 tmpArr
2347 end daeExpCrefRhsArrayBoxXml;
2348
2349 ✗ template daeExpRecordCrefRhsXml(DAE.Type ty, ComponentRef cr, Context context, Text &preExp /*BUFP*/,
2350 Text &varDecls /*BUFP*/)
2351 ::=
2352 match ty
2353 case T_COMPLEX(complexClassType = record_state, varLst = var_lst) then
2354 let vars = var_lst |> v => daeExpXml(makeCrefRecordExp(cr,v), context, &preExp, &varDecls)
2355 ;separator="\n "
2356 <<
2357 <%vars%>
2358 >>
2359 end daeExpRecordCrefRhsXml;
2360
2361
2362 /*********************************************************************
2363 *********************************************************************
2364 * LEFT HAND SIDE
2365 *********************************************************************
2366 *********************************************************************/
2367
2368 /*
2369 * adrpo:2011-06-25: NOTE that Lhs generates afterExp not preExp!
2370 * Also, all the causality is REVERSED, meaning
2371 * that if for RHS x = y for LHS y = x;
2372 */
2373
2374
2375 ✗ template daeExpCrefLhsXml(Exp exp, Context context, Text &afterExp /*BUFP*/,
2376 Text &varDecls /*BUFP*/)
2377 "Generates code for a component reference on the left hand side of an expression."
2378 ::=
2379 match exp
2380 // A record cref without subscripts (i.e. a record instance) is handled
2381 // by daeExpRecordCrefLhs only in a simulation context, not in a function.
2382 case CREF(componentRef = cr, ty = t as T_COMPLEX(complexClassType = RECORD(path = _))) then
2383 match context case FUNCTION_CONTEXT(__) then
2384 daeExpCrefLhs2Xml(exp, context, &afterExp, &varDecls)
2385 else
2386 daeExpRecordCrefLhsXml(t, cr, context, &afterExp /*BUFC*/, &varDecls /*BUFD*/)
2387 case CREF(componentRef = cr, ty = T_FUNCTION_REFERENCE_FUNC(__)) then
2388 <<
2389 <%crefFunctionNameXml(cr)%>
2390 >>
2391
2392 case CREF(componentRef = cr, ty = T_FUNCTION_REFERENCE_VAR(__)) then
2393 <<
2394 <%crefStrXml(cr)%>
2395 >>
2396 else daeExpCrefLhs2Xml(exp, context, &afterExp, &varDecls)
2397 end daeExpCrefLhsXml;
2398
2399 ✗ template daeExpCrefLhs2Xml(Exp ecr, Context context, Text &afterExp /*BUFP*/,
2400 Text &varDecls /*BUFP*/)
2401 "Generates XML code for a component reference on the left hand side!"
2402 ::=
2403 match ecr
2404 case ecr as CREF(componentRef=cr, ty=ty) then
2405 let &afterExp += '/* daeExpCrefLhs2 begin afterExp (<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) */<%\n%>'
2406 let box = daeExpCrefLhsArrayBoxXml(ecr, context, &afterExp, &varDecls)
2407 if box then
2408 box
2409 else
2410 if crefIsScalar(cr, context)
2411 then
2412 <<
2413 <%contextCrefXml(cr,context)%>
2414 >>
2415
2416 else
2417 if crefSubIsScalar(cr)
2418 then
2419 // The array subscript results in a scalar
2420 let &afterExp += '/* daeExpCrefLhs2 SCALAR(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) afterExp */<%\n%>'
2421 let arrName = contextCrefXml(crefStripLastSubs(cr), context)
2422 let arrayType = expTypeArrayXml(ty)
2423 let dimsLenStr = listLength(crefSubs(cr))
2424 let dimsValuesStr = (crefSubs(cr) |> INDEX(__) =>
2425 daeExpXml(exp, context, &afterExp /*BUFC*/, &varDecls /*BUFD*/)
2426 ;separator="\n")
2427 match arrayType
2428 case "metatype_array" then
2429 'arrayGet(<%arrName%>,<%dimsValuesStr%>) /* DAE.CREF */'
2430 else
2431 <<
2432 <exp:Identifier>
2433 <exp:QualifiedNamePart name="<%arrName%>">
2434 <exp:ArraySubscripts>
2435 <exp:IndexExpression>
2436 <%dimsValuesStr%>
2437 </exp:IndexExpression>
2438 </exp:ArraySubscripts>
2439 </exp:QualifiedNamePart>
2440 </exp:Identifier>
2441 >>
2442 else
2443 // The array subscript denotes a slice
2444 let &afterExp += '/* daeExpCrefLhs2 SLICE(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) afterExp */<%\n%>'
2445 let arrName = contextArrayCrefXml(cr, context)
2446 let arrayType = expTypeArrayXml(ty)
2447 let tmp = tempDeclXml(arrayType, &varDecls /*BUFD*/)
2448 let spec1 = daeExpCrefLhsIndexSpecXml(crefSubs(cr), context, &afterExp /*BUFC*/, &varDecls /*BUFD*/)
2449 let &afterExp += 'indexed_assign_<%arrayType%>(&<%tmp%>, &<%arrName%>, &<%spec1%>);<%\n%>'
2450 tmp
2451
2452 case ecr then
2453 let &afterExp += '/* daeExpCrefLhs2 UNHANDLED(<%ExpressionDumpTpl.dumpExp(ecr,"\"")%>) afterExp */<%\n%>'
2454 <<
2455 /* SimCodeC.tpl template: daeExpCrefLhs2: UNHANDLED EXPRESSION:
2456 * <%ExpressionDumpTpl.dumpExp(ecr,"\"")%>
2457 */
2458 >>
2459 end daeExpCrefLhs2Xml;
2460
2461 ✗ template daeExpCrefLhsIndexSpecXml(list<DAE.Subscript> subs, Context context,
2462 Text &afterExp /*BUFP*/, Text &varDecls /*BUFP*/)
2463 "Helper to daeExpCrefLhs."
2464 ::=
2465 let nridx_str = listLength(subs)
2466 let idx_str = (subs |> sub =>
2467 match sub
2468 case INDEX(__) then
2469 let expPart = daeExpXml(exp, context, &afterExp /*BUFC*/, &varDecls /*BUFD*/)
2470 let str = <<(0), make_index_array(1, (int) <%expPart%>), 'S'>>
2471 str
2472 case WHOLEDIM(__) then
2473 let str = <<(1), (int*)0, 'W'>>
2474 str
2475 case SLICE(__) then
2476 let expPart = daeExpXml(exp, context, &afterExp /*BUFC*/, &varDecls /*BUFD*/)
2477 let tmp = tempDeclXml("modelica_integer", &varDecls /*BUFD*/)
2478 let &afterExp += '<%tmp%> = size_of_dimension_integer_array(&<%expPart%>, 1);<%\n%>'
2479 let str = <<<%tmp%>, integer_array_make_index_array(<%expPart%>), 'A'>>
2480 str
2481 ;separator=", ")
2482 let tmp = tempDeclXml("index_spec_t", &varDecls /*BUFD*/)
2483 let &afterExp += 'create_index_spec(&<%tmp%>, <%nridx_str%>, <%idx_str%>);<%\n%>'
2484 tmp
2485 end daeExpCrefLhsIndexSpecXml;
2486
2487 ✗ template daeExpCrefLhsArrayBoxXml(Exp ecr, Context context, Text &afterExp /*BUFP*/,
2488 Text &varDecls /*BUFP*/)
2489 "Helper to daeExpCrefLhs."
2490 ::=
2491 match ecr
2492 case ecr as CREF(ty=T_ARRAY(ty=aty,dims=dims)) then
2493 match context
2494 case FUNCTION_CONTEXT(__) then ''
2495 else
2496 // For context simulation and other array variables must be boxed into a real_array
2497 // object since they are represented only in a double array.
2498 let tmpArr = tempDeclXml(expTypeArrayXml(aty), &varDecls /*BUFD*/)
2499 let dimsLenStr = listLength(dims)
2500 let dimsValuesStr = (dims |> dim => dimensionXml(dim) ;separator=", ")
2501 let type = expTypeShortXml(aty)
2502 let &afterExp += '<%type%>_array_create(&<%tmpArr%>, ((modelica_<%type%>*)&(<%arrayCrefXmlStr(ecr.componentRef)%>)), <%dimsLenStr%>, <%dimsValuesStr%>);<%\n%>'
2503 tmpArr
2504 end daeExpCrefLhsArrayBoxXml;
2505
2506 ✗ template daeExpRecordCrefLhsXml(DAE.Type ty, ComponentRef cr, Context context, Text &afterExp /*BUFP*/,
2507 Text &varDecls /*BUFP*/)
2508 ::=
2509 match ty
2510 case T_COMPLEX(complexClassType = record_state, varLst = var_lst) then
2511 let vars = var_lst |> v => daeExpXml(makeCrefRecordExp(cr,v), context, &afterExp, &varDecls)
2512 ;separator=", "
2513 let record_type_name = underscorePathXml(ClassInfUtil.getStateName(record_state))
2514 let ret_type = '<%record_type_name%>_rettype'
2515 let ret_var = tempDeclXml(ret_type, &varDecls)
2516 let &afterExp += '<%ret_var%> = _<%record_type_name%>(<%vars%>);<%\n%>'
2517 '<%ret_var%>.<%ret_type%>_1'
2518 end daeExpRecordCrefLhsXml;
2519
2520 /*********************************************************************
2521 *********************************************************************
2522 * DONE RHS and LHS
2523 *********************************************************************
2524 *********************************************************************/
2525
2526 98 template daeExpBinaryXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2527 Text &varDecls /*BUFP*/)
2528 "Generates code for a binary expression."
2529 ::=
2530
2531 match exp
2532 case BINARY(__) then
2533 let e1 = daeExpXml(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2534 let e2 = daeExpXml(exp2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2535 match operator
2536 case ADD(__) then
2537 <<
2538 <exp:Add>
2539 <%e1%>
2540 <%e2%>
2541 </exp:Add>
2542 >>
2543 case SUB(__) then
2544 <<
2545 <exp:Sub>
2546 <%e1%>
2547 <%e2%>
2548 </exp:Sub>
2549 >>
2550 case MUL(__) then
2551 <<
2552 <exp:Mul>
2553 <%e1%>
2554 <%e2%>
2555 </exp:Mul>
2556 >>
2557 case DIV(__) then
2558 <<
2559 <exp:Div>
2560 <%e1%>
2561 <%e2%>
2562 </exp:Div>
2563 >>
2564 case POW(__) then
2565 <<
2566 <exp:Pow>
2567 <%e1%>
2568 <%e2%>
2569 </exp:Pow>
2570 >>
2571 case UMINUS(__) then daeExpUnaryXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2572 case ADD_ARR(__) then
2573 let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
2574 case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
2575 else "real_array"
2576 let var = tempDeclXml(type, &varDecls /*BUFD*/)
2577 let &preExp +=
2578 <<
2579 <exp:Add>
2580 <%e1%>
2581 <%e2%>
2582 </exp:Add><%\n%>
2583 >>
2584 '<%var%>'
2585 case SUB_ARR(__) then
2586 let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
2587 case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
2588 else "real_array"
2589 let var = tempDeclXml(type, &varDecls /*BUFD*/)
2590 let &preExp +=
2591 <<
2592 <exp:Sub>
2593 <%e1%>
2594 <%e2%>
2595 </exp:Sub><%\n%>
2596 >>
2597 '<%var%>'
2598 case MUL_ARR(__) then 'daeExpBinary:ERR for MUL_ARR'
2599 case DIV_ARR(__) then 'daeExpBinary:ERR for DIV_ARR'
2600 case MUL_ARRAY_SCALAR(__) then
2601 let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
2602 case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
2603 else "real_array"
2604 let var = tempDeclXml(type, &varDecls /*BUFD*/)
2605 let &preExp +=
2606 <<
2607 <exp:Mul>
2608 <%e1%>
2609 <%e2%>
2610 </exp:Mul><%\n%>
2611 >>
2612 '<%var%>'
2613 case ADD_ARRAY_SCALAR(__) then 'daeExpBinary:ERR for ADD_ARRAY_SCALAR'
2614 case SUB_SCALAR_ARRAY(__) then 'daeExpBinary:ERR for SUB_SCALAR_ARRAY'
2615 case MUL_SCALAR_PRODUCT(__) then
2616 let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_scalar"
2617 case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_scalar"
2618 else "real_scalar"
2619 'mul_<%type%>_product(&<%e1%>, &<%e2%>)'
2620 case MUL_MATRIX_PRODUCT(__) then
2621 let typeShort = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer"
2622 case T_ARRAY(ty=T_ENUMERATION(__)) then "integer"
2623 else "real"
2624 let type = '<%typeShort%>_array'
2625 let var = tempDeclXml(type, &varDecls /*BUFD*/)
2626 let &preExp +=
2627 <<
2628 <exp:Mul>
2629 <%e1%>
2630 <%e2%>
2631 </exp:Mul><%\n%>
2632 >>
2633 '<%var%>'
2634 case DIV_ARRAY_SCALAR(__) then
2635 let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
2636 case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
2637 else "real_array"
2638 let var = tempDeclXml(type, &varDecls /*BUFD*/)
2639 let &preExp +=
2640 <<
2641 <exp:Div>
2642 <%e1%>
2643 <%e2%>
2644 </exp:Div><%\n%>
2645 >>
2646 '<%var%>'
2647 case DIV_SCALAR_ARRAY(__) then 'daeExpBinary:ERR for DIV_SCALAR_ARRAY'
2648 case POW_ARRAY_SCALAR(__) then 'daeExpBinary:ERR for POW_ARRAY_SCALAR'
2649 case POW_SCALAR_ARRAY(__) then 'daeExpBinary:ERR for POW_SCALAR_ARRAY'
2650 case POW_ARR(__) then 'daeExpBinary:ERR for POW_ARR'
2651 case POW_ARR2(__) then 'daeExpBinary:ERR for POW_ARR2'
2652 else "daeExpBinary:ERR"
2653 end daeExpBinaryXml;
2654
2655 3 template daeExpUnaryXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2656 Text &varDecls /*BUFP*/)
2657 "Generates code for a unary expression."
2658 ::=
2659 match exp
2660 case UNARY(__) then
2661 let e = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2662 match operator
2663 case UMINUS(__) then
2664 <<
2665 <exp:Neg>
2666 <%e%>
2667 </exp:Neg>
2668 >>
2669 case UMINUS_ARR(ty=T_ARRAY(ty=T_REAL(__))) then
2670 <<
2671 <exp:Neg>
2672 <%e%>
2673 </exp:Neg>
2674 >>
2675 case UMINUS_ARR(__) then error(sourceInfo(),"unary minus for non-real arrays not implemented")
2676 else error(sourceInfo(),"daeExpUnary:ERR")
2677 end daeExpUnaryXml;
2678
2679 ✗ template daeExpLbinaryXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2680 Text &varDecls /*BUFP*/)
2681 "Generates code for a logical binary expression."
2682 ::=
2683 match exp
2684 case LBINARY(__) then
2685 let e1 = daeExpXml(exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2686 let e2 = daeExpXml(exp2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2687 match operator
2688 case AND(__) then
2689 <<
2690 <exp:And>
2691 <%e1%>
2692 <%e2%>
2693 </exp:And>
2694 >>
2695 case OR(__) then
2696 <<
2697 <exp:Or>
2698 <%e1%>
2699 <%e2%>
2700 </exp:Or>
2701 >>
2702 else "daeExpLbinary:ERR"
2703 end daeExpLbinaryXml;
2704
2705 ✗ template daeExpLunaryXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2706 Text &varDecls /*BUFP*/)
2707 "Generates code for a logical unary expression."
2708 ::=
2709 match exp
2710 case LUNARY(__) then
2711 let e = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2712 match operator
2713 case NOT(__) then
2714 <<
2715 <exp:Not>
2716 <%e%>
2717 </exp:Not>
2718 >>
2719 end daeExpLunaryXml;
2720
2721 16 template daeExpRelationXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2722 Text &varDecls /*BUFP*/)
2723 "Generates code for a relation expression."
2724 ::=
2725 match exp
2726 case rel as RELATION(__) then
2727 let simRel = daeExpRelationSimXml(rel, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2728 if simRel then
2729 simRel
2730 else
2731 let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2732 let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
2733 match rel.operator
2734
2735 case LESS(ty = T_BOOL(__)) then '(!<%e1%> && <%e2%>)'
2736 case LESS(ty = T_STRING(__)) then '(stringCompare(<%e1%>, <%e2%>) < 0)'
2737 case LESS(__) then
2738 <<
2739 <exp:LogLt>
2740 <%e1%>
2741 <%e2%>
2742 </exp:LogLt>
2743 >>
2744 case GREATER(ty = T_BOOL(__)) then '(<%e1%> && !<%e2%>)'
2745 case GREATER(ty = T_STRING(__)) then '(stringCompare(<%e1%>, <%e2%>) > 0)'
2746 case GREATER(__) then
2747 <<
2748 <exp:LogGt>
2749 <%e1%>
2750 <%e2%>
2751 </exp:LogGt>
2752 >>
2753 case LESSEQ(ty = T_BOOL(__)) then '(!<%e1%> || <%e2%>)'
2754 case LESSEQ(ty = T_STRING(__)) then '(stringCompare(<%e1%>, <%e2%>) <= 0)'
2755 case LESSEQ(__) then
2756 <<
2757 <exp:LogLeq>
2758 <%e1%>
2759 <%e2%>
2760 </exp:LogLeq>
2761 >>
2762 case GREATEREQ(ty = T_BOOL(__)) then '(<%e1%> || !<%e2%>)'
2763 case GREATEREQ(ty = T_STRING(__)) then '(stringCompare(<%e1%>, <%e2%>) >= 0)'
2764 case GREATEREQ(__) then
2765 <<
2766 <exp:LogGeq>
2767 <%e1%>
2768 <%e2%>
2769 </exp:LogGeq>
2770 >>
2771 case EQUAL(ty = T_BOOL(__)) then '((!<%e1%> && !<%e2%>) || (<%e1%> && <%e2%>))'
2772 case EQUAL(ty = T_STRING(__)) then '(stringEqual(<%e1%>, <%e2%>))'
2773 case EQUAL(__) then
2774 <<
2775 <exp:LogEq>
2776 <%e1%>
2777 <%e2%>
2778 </exp:LogEq>
2779 >>
2780 case NEQUAL(ty = T_BOOL(__)) then '((!<%e1%> && <%e2%>) || (<%e1%> && !<%e2%>))'
2781 case NEQUAL(ty = T_STRING(__)) then '(!stringEqual(<%e1%>, <%e2%>))'
2782 case NEQUAL(__) then
2783 <<
2784 <exp:LogNeq>
2785 <%e1%>
2786 <%e2%>
2787 </exp:LogNeq>
2788 >>
2789 else "daeExpRelation:ERR"
2790
2791 end daeExpRelationXml;
2792
2793 16 template daeExpRelationSimXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2794 Text &varDecls /*BUFP*/)
2795 "Helper to daeExpRelation."
2796 ::=
2797 match exp
2798 case rel as RELATION(__) then
2799 match context
2800 case SIMULATION_CONTEXT(genDiscrete=false) then
2801 match rel.optionExpisASUB
2802 case NONE() then
2803 let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2804 let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2805 let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/)
2806 match rel.operator
2807 case LESS(__) then
2808 let &preExp +=
2809 <<
2810 <exp:LogLt>
2811 <%e1%>
2812 <%e2%>
2813 </exp:LogLt><%\n%>
2814 >>
2815 res
2816 case LESSEQ(__) then
2817 let &preExp +=
2818 <<
2819 <exp:LogLeq>
2820 <%e1%>
2821 <%e2%>
2822 </exp:LogLeq><%\n%>
2823 >>
2824 res
2825 case GREATER(__) then
2826 let &preExp +=
2827 <<
2828 <exp:LogGt>
2829 <%e1%>
2830 <%e2%>
2831 </exp:LogGt><%\n%>
2832 >>
2833 res
2834 case GREATEREQ(__) then
2835 let &preExp +=
2836 <<
2837 <exp:LogGeq>
2838 <%e1%>
2839 <%e2%>
2840 </exp:LogGeq><%\n%>
2841 >>
2842 res
2843 end match
2844 case SOME((exp,i,j)) then
2845 let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2846 let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2847 let iterator = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2848 let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/)
2849 //let e3 = daeExp(createArray(i), context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2850 match rel.operator
2851 case LESS(__) then
2852 let &preExp +=
2853 <<
2854 <exp:LogLt>
2855 <%e1%>
2856 <%e2%>
2857 </exp:LogLt><%\n%>
2858 >>
2859 res
2860 case LESSEQ(__) then
2861 let &preExp +=
2862 <<
2863 <exp:LogLeq>
2864 <%e1%>
2865 <%e2%>
2866 <exp:LogLeq><%\n%>
2867 >>
2868 res
2869 case GREATER(__) then
2870 let &preExp +=
2871 <<
2872 <exp:LogGt>
2873 <%e1%>
2874 <%e2%>
2875 </exp:LogGt><%\n%>
2876 >>
2877 res
2878 case GREATEREQ(__) then
2879 let &preExp +=
2880 <<
2881 <exp:LogGeq>
2882 <%e1%>
2883 <%e2%>
2884 </exp:LogGeq><%\n%>
2885 >>
2886 res
2887 end match
2888 end match
2889 case SIMULATION_CONTEXT(genDiscrete=true) then
2890 match rel.optionExpisASUB
2891 case NONE() then
2892 let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2893 let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2894 let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/)
2895 match rel.operator
2896 case LESS(__) then
2897 let &preExp +=
2898 <<
2899 <exp:LogLt>
2900 <%e1%>
2901 <%e2%>
2902 </exp:LogLt><%\n%>
2903 >>
2904 res
2905 case LESSEQ(__) then
2906 let &preExp +=
2907 <<
2908 <exp:LogLeq>
2909 <%e1%>
2910 <%e2%>
2911 </exp:LogLeq><%\n%>
2912 >>
2913 res
2914 case GREATER(__) then
2915 let &preExp +=
2916 <<
2917 <exp:LogGt>
2918 <%e1%>
2919 <%e2%>
2920 </exp:LogGt><%\n%>
2921 >>
2922 res
2923 case GREATEREQ(__) then
2924 let &preExp +=
2925 <<
2926 <exp:LogGeq>
2927 <%e1%>
2928 <%e2%>
2929 </exp:LogGeq><%\n%>
2930 >>
2931 res
2932 end match
2933 case SOME((exp,i,j)) then
2934 let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2935 let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2936 let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/)
2937 //let e3 = daeExp(createArray(i), context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2938 let iterator = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2939 match rel.operator
2940 case LESS(__) then
2941 let &preExp +=
2942 <<
2943 <exp:LogLt>
2944 <%e1%>
2945 <%e2%>
2946 </exp:LogLt><%\n%>
2947 >>
2948 res
2949 case LESSEQ(__) then
2950 let &preExp +=
2951 <<
2952 <exp:LogLeq>
2953 <%e1%>
2954 <%e2%>
2955 </exp:LogLeq><%\n%>
2956 >>
2957 res
2958 case GREATER(__) then
2959 let &preExp +=
2960 <<
2961 <exp:LogGt>
2962 <%e1%>
2963 <%e2%>
2964 </exp:LogGt><%\n%>
2965 >>
2966 res
2967 case GREATEREQ(__) then
2968 let &preExp +=
2969 <<
2970 <exp:LogGeq>
2971 <%e1%>
2972 <%e2%>
2973 </exp:LogGeq><%\n%>
2974 >>
2975 res
2976 end match
2977 end match
2978 end match
2979 end match
2980 end daeExpRelationSimXml;
2981
2982 ✗ template daeExpConstraintXml(Exp exp, Context context, Text &preExp /*BUFP*/,
2983 Text &varDecls /*BUFP*/)
2984 "Generates XML for constraint"
2985 ::=
2986 match exp
2987 case rel as RELATION(__) then
2988 match context
2989 case SIMULATION_CONTEXT(genDiscrete=true) then
2990 match rel.optionExpisASUB
2991 case NONE() then
2992 let e1 = daeExpXml(rel.exp1, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2993 let e2 = daeExpXml(rel.exp2, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
2994 let res = tempDeclXml("modelica_boolean", &varDecls /*BUFC*/)
2995 match rel.operator
2996 case EQUAL(__) then
2997 <<
2998 <opt:ConstraintEqu>
2999 <%e1%>
3000 <%e2%>
3001 </opt:ConstraintEqu><%\n%>
3002 >>
3003 case LESSEQ(__) then
3004 <<
3005 <opt:ConstraintLeq>
3006 <%e1%>
3007 <%e2%>
3008 </opt:ConstraintLeq><%\n%>
3009 >>
3010 case GREATEREQ(__) then
3011 <<
3012 <opt:ConstraintGeq>
3013 <%e1%>
3014 <%e2%>
3015 </opt:ConstraintGeq><%\n%>
3016 >>
3017 else
3018 <<
3019 "The XML schema does only support =, >= , <= operators for constraints"
3020 >>
3021 end match
3022 end match
3023 end match
3024 end match
3025 end daeExpConstraintXml;
3026
3027 14 template daeExpIfXml(Exp exp, Context context, Text &preExp /*BUFP*/,
3028 Text &varDecls /*BUFP*/)
3029 "Generates code for an if expression."
3030 ::=
3031 match exp
3032 case IFEXP(__) then
3033 let &preExpCond = buffer ""
3034 let condExp = daeExpXml(expCond, context, &preExpCond, &varDecls /*BUFD*/)
3035 let &resVar = buffer ""
3036 let &preExpThen = buffer ""
3037 let eThen = daeExpXml(expThen, context, &preExpThen, &varDecls /*BUFD*/)
3038 let &preExpElse = buffer ""
3039 let eElse = daeExpXml(expElse, context, &preExpElse, &varDecls /*BUFD*/)
3040 let &preExp +=
3041 <<
3042 <fun:If>
3043 <fun:Condition>
3044 <%condExp%>
3045 </fun:Condition>
3046 <fun:Statements>
3047 <%eThen%>
3048 </fun:Statements>
3049 <fun:Else>
3050 <%eElse%>
3051 </fun:Else>
3052 </fun:If>
3053 >>
3054 resVar
3055 end daeExpIfXml;
3056
3057 6 template daeExpCallXml(Exp call, Context context, Text &preExp /*BUFP*/,
3058 Text &varDecls /*BUFP*/)
3059 "Generates code for a function call."
3060 ::=
3061 match call
3062 // special builtins
3063 case CALL(path=IDENT(name="DIVISION"),
3064 expLst={e1, e2, DAE.SCONST(string=string)}) then
3065 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3066 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3067 let var3 = Util.escapeModelicaStringToXmlString(string)
3068 <<
3069 <exp:Div>
3070 <%var1%>
3071 <%var2%>
3072 </exp:Div>
3073 >>
3074 case CALL(attr=CALL_ATTR(ty=ty),
3075 path=IDENT(name="DIVISION_ARRAY_SCALAR"),
3076 expLst={e1, e2, e3 as SHARED_LITERAL(__)}) then
3077 let type = match ty case T_ARRAY(ty=T_INTEGER(__)) then "integer_array"
3078 case T_ARRAY(ty=T_ENUMERATION(__)) then "integer_array"
3079 else "real_array"
3080 let var = tempDeclXml(type, &varDecls)
3081 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3082 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3083 let var3 = daeExpXml(e3, context, &preExp, &varDecls)
3084 let &preExp += 'division_alloc_<%type%>_scalar(&<%var1%>, <%var2%>, &<%var%>, <%var3%>);<%\n%>'
3085 '<%var%>'
3086
3087 case exp as CALL(path=IDENT(name="DIVISION_ARRAY_SCALAR")) then
3088 error(sourceInfo(), 'Code generation does not support <%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
3089
3090 case CALL(path=IDENT(name="der"), expLst={arg as CREF(__)}) then
3091 <<
3092 <exp:Der>
3093 <%crefXml(arg.componentRef)%>
3094 </exp:Der>
3095 >>
3096 case CALL(path=IDENT(name="der"), expLst={exp}) then
3097 error(sourceInfo(), 'Code generation does not support der(<%ExpressionDumpTpl.dumpExp(exp,"\"")%>)')
3098 case CALL(path=IDENT(name="pre"), expLst={arg}) then
3099 daeExpCallPreXml(arg, context, preExp, varDecls)
3100 case CALL(path=IDENT(name="edge"), expLst={arg as CREF(__)}) then
3101 <<
3102 <%crefXml(arg.componentRef)%>
3103 >>
3104 case CALL(path=IDENT(name="edge"), expLst={exp}) then
3105 error(sourceInfo(), 'Code generation does not support edge(<%ExpressionDumpTpl.dumpExp(exp,"\"")%>)')
3106 case CALL(path=IDENT(name="change"), expLst={arg as CREF(__)}) then
3107 <<
3108 <%crefXml(arg.componentRef)%>
3109 >>
3110 case CALL(path=IDENT(name="change"), expLst={exp}) then
3111 error(sourceInfo(), 'Code generation does not support change(<%ExpressionDumpTpl.dumpExp(exp,"\"")%>)')
3112
3113 case CALL(path=IDENT(name="print"), expLst={e1}) then
3114 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3115 if acceptMetaModelicaGrammar() then 'print(<%var1%>)' else 'puts(<%var1%>)'
3116
3117 case CALL(path=IDENT(name="max"), attr=CALL_ATTR(ty = T_REAL(__)), expLst={e1,e2}) then
3118 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3119 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3120 <<
3121 <exp:Max>
3122 <%var1%>
3123 <%var2%>
3124 </exp:Max>
3125 >>
3126
3127 case CALL(path=IDENT(name="max"), expLst={e1,e2}) then
3128 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3129 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3130 <<
3131 <exp:Max>
3132 <%var1%>
3133 <%var2%>
3134 </exp:Max>
3135 >>
3136
3137 case CALL(path=IDENT(name="sum"), attr=CALL_ATTR(ty = ty), expLst={e}) then
3138 let arr = daeExpXml(e, context, &preExp, &varDecls)
3139 let ty_str = '<%expTypeArrayXml(ty)%>'
3140 'sum_<%ty_str%>(&<%arr%>)'
3141
3142 case CALL(path=IDENT(name="min"), attr=CALL_ATTR(ty = T_REAL(__)), expLst={e1,e2}) then
3143 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3144 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3145 <<
3146 <exp:Min>
3147 <%var1%>
3148 <%var2%>
3149 </exp:Min>
3150 >>
3151 case CALL(path=IDENT(name="min"), expLst={e1,e2}) then
3152 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3153 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3154 <<
3155 <exp:Min>
3156 <%var1%>
3157 <%var2%>
3158 </exp:Min>
3159 >>
3160
3161 case CALL(path=IDENT(name="abs"), expLst={e1}, attr=CALL_ATTR(ty = T_INTEGER(__))) then
3162 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3163 <<
3164 <exp:Abs>
3165 <%var1%>
3166 </exp:Abs>
3167 >>
3168
3169 case CALL(path=IDENT(name="abs"), expLst={e1}) then
3170 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3171 <<
3172 <exp:Abs>
3173 <%var1%>
3174 </exp:Abs>
3175 >>
3176
3177 //sqrt
3178 case CALL(path=IDENT(name="sqrt"), expLst={e1}, attr=attr as CALL_ATTR(__)) then
3179 let retPre = assertCommonXml(createAssertforSqrt(e1),createDAEString("Model error: Argument of sqrt should be >= 0"), context, &varDecls, Absyn.dummyInfo)
3180 let argStr = daeExpXml(e1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3181 let &preExp += '<%retPre%>'
3182 <<
3183 <exp:Sqrt>
3184 <%argStr%>
3185 </exp:Sqrt>
3186 >>
3187
3188 case CALL(path=IDENT(name="div"), expLst={e1,e2}, attr=CALL_ATTR(ty = T_INTEGER(__))) then
3189 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3190 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3191 <<
3192 <exp:Div>
3193 <%var1%>
3194 <%var2%>
3195 </exp:Div>
3196 >>
3197 case CALL(path=IDENT(name="div"), expLst={e1,e2}) then
3198 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3199 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3200 <<
3201 <exp:Div>
3202 <%var1%>
3203 <%var2%>
3204 </exp:Div>
3205 >>
3206
3207 case CALL(path=IDENT(name="mod"), expLst={e1,e2}, attr=CALL_ATTR(ty = ty)) then
3208 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3209 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3210 'modelica_mod_<%expTypeShortXml(ty)%>(<%var1%>,<%var2%>)'
3211
3212 case CALL(path=IDENT(name="max"), attr=CALL_ATTR(ty = ty), expLst={array}) then
3213 let expVar = daeExpXml(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3214 let arr_tp_str = '<%expTypeArrayXml(ty)%>'
3215 let tvar = tempDeclXml(expTypeModelicaXml(ty), &varDecls /*BUFD*/)
3216 let &preExp += '<%tvar%> = max_<%arr_tp_str%>(&<%expVar%>);<%\n%>'
3217 '<%tvar%>'
3218
3219 case CALL(path=IDENT(name="min"), attr=CALL_ATTR(ty = ty), expLst={array}) then
3220 let expVar = daeExpXml(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3221 let arr_tp_str = '<%expTypeArrayXml(ty)%>'
3222 let tvar = tempDeclXml(expTypeModelicaXml(ty), &varDecls /*BUFD*/)
3223 let &preExp += '<%tvar%> = min_<%arr_tp_str%>(&<%expVar%>);<%\n%>'
3224 '<%tvar%>'
3225
3226 case CALL(path=IDENT(name="fill"), expLst=val::dims, attr=CALL_ATTR(ty = ty)) then
3227 let valExp = daeExpXml(val, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3228 let dimsExp = (dims |> dim =>
3229 daeExpXml(dim, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) ;separator=", ")
3230 let ty_str = '<%expTypeArrayXml(ty)%>'
3231 let tvar = tempDeclXml(ty_str, &varDecls /*BUFD*/)
3232 let &preExp += 'fill_alloc_<%ty_str%>(&<%tvar%>, <%valExp%>, <%listLength(dims)%>, <%dimsExp%>);<%\n%>'
3233 '<%tvar%>'
3234
3235 case call as CALL(path=IDENT(name="vector")) then
3236 error(sourceInfo(),'vector() call does not have a C implementation <%ExpressionDumpTpl.dumpExp(call,"\"")%>')
3237
3238 case CALL(path=IDENT(name="cat"), expLst=dim::arrays, attr=CALL_ATTR(ty = ty)) then
3239 let dim_exp = daeExpXml(dim, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3240 let arrays_exp = (arrays |> array =>
3241 daeExpXml(array, context, &preExp /*BUFC*/, &varDecls /*BUFD*/) ;separator=", &")
3242 let ty_str = '<%expTypeArrayXml(ty)%>'
3243 let tvar = tempDeclXml(ty_str, &varDecls /*BUFD*/)
3244 let &preExp += 'cat_alloc_<%ty_str%>(<%dim_exp%>, &<%tvar%>, <%listLength(arrays)%>, &<%arrays_exp%>);<%\n%> where is cat2'
3245 '<%tvar%>'
3246
3247 case CALL(path=IDENT(name="promote"), expLst={A, n}) then
3248 let var1 = daeExpXml(A, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3249 let var2 = daeExpXml(n, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3250 let arr_tp_str = '<%expTypeFromExpArrayXml(A)%>'
3251 let tvar = tempDeclXml(arr_tp_str, &varDecls /*BUFD*/)
3252 let &preExp += 'promote_alloc_<%arr_tp_str%>(&<%var1%>, <%var2%>, &<%tvar%>);<%\n%>'
3253 '<%tvar%>'
3254
3255 case CALL(path=IDENT(name="transpose"), expLst={A}) then
3256 let var1 = daeExpXml(A, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3257 let arr_tp_str = '<%expTypeFromExpArrayXml(A)%>'
3258 let tvar = tempDeclXml(arr_tp_str, &varDecls /*BUFD*/)
3259 let &preExp += 'transpose_alloc_<%arr_tp_str%>(&<%var1%>, &<%tvar%>);<%\n%>'
3260 '<%tvar%>'
3261
3262 case CALL(path=IDENT(name="cross"), expLst={v1, v2}) then
3263 let var1 = daeExpXml(v1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3264 let var2 = daeExpXml(v2, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3265 let arr_tp_str = '<%expTypeFromExpArrayXml(v1)%>'
3266 let tvar = tempDeclXml(arr_tp_str, &varDecls /*BUFD*/)
3267 let &preExp += 'cross_alloc_<%arr_tp_str%>(&<%var1%>, &<%var2%>, &<%tvar%>);<%\n%>'
3268 '<%tvar%>'
3269
3270 case CALL(path=IDENT(name="identity"), expLst={A}) then
3271 let var1 = daeExpXml(A, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3272 let arr_tp_str = '<%expTypeFromExpArrayXml(A)%>'
3273 let tvar = tempDeclXml(arr_tp_str, &varDecls /*BUFD*/)
3274 let &preExp += 'identity_alloc_<%arr_tp_str%>(<%var1%>, &<%tvar%>);<%\n%>'
3275 '<%tvar%>'
3276
3277 case CALL(path=IDENT(name="rem"), expLst={e1, e2}) then
3278 let var1 = daeExpXml(e1, context, &preExp, &varDecls)
3279 let var2 = daeExpXml(e2, context, &preExp, &varDecls)
3280 let typeStr = expTypeFromExpShortXml(e1)
3281 'modelica_rem_<%typeStr%>(<%var1%>,<%var2%>)'
3282
3283 /*
3284 case CALL(path=IDENT(name="String"), expLst={s, format}) then
3285 let tvar = tempDeclXml("modelica_string", &varDecls /*BUFD*/)
3286 let sExp = daeExpXml(s, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3287
3288 let formatExp = daeExpXml(format, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3289 let typeStr = expTypeFromExpModelicaXml(s)
3290 let &preExp += '<%tvar%> = <%typeStr%>_to_modelica_string_format(<%sExp%>, <%formatExp%>);<%\n%>'
3291 '<%tvar%>'
3292
3293 case CALL(path=IDENT(name="String"), expLst={s, minlen, leftjust}) then
3294 let tvar = tempDeclXml("modelica_string", &varDecls /*BUFD*/)
3295 let sExp = daeExpXml(s, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3296 let minlenExp = daeExpXml(minlen, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3297 let leftjustExp = daeExpXml(leftjust, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3298 let typeStr = expTypeFromExpModelicaXml(s)
3299 let &preExp += '<%tvar%> = <%typeStr%>_to_modelica_string(<%sExp%>, <%minlenExp%>, <%leftjustExp%>);<%\n%>'
3300 '<%tvar%>'
3301
3302 case CALL(path=IDENT(name="String"), expLst={s, signdig, minlen, leftjust}) then
3303 let tvar = tempDeclXml("modelica_string", &varDecls /*BUFD*/)
3304 let sExp = daeExpXml(s, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3305 let minlenExp = daeExpXml(minlen, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3306 let leftjustExp = daeExpXml(leftjust, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3307 let signdigExp = daeExpXml(signdig, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3308 let &preExp += '<%tvar%> = modelica_real_to_modelica_string(<%sExp%>, <%signdigExp%>, <%minlenExp%>, <%leftjustExp%>);<%\n%>'
3309 '<%tvar%>'
3310 */
3311
3312 case CALL(path=IDENT(name="delay"), expLst={ICONST(integer=index), e, d, delayMax}) then
3313 let var1 = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3314 let var2 = daeExpXml(d, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3315 let var3 = daeExpXml(delayMax, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3316 <<
3317 <exp:Delay>
3318 <%var1%>
3319 <%var2%>
3320 <%var3%>
3321 </exp:Delay>
3322 >>
3323 case CALL(path=IDENT(name="integer"), expLst={toBeCasted}) then
3324 let castedVar = daeExpXml(toBeCasted, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3325 '<%castedVar%>'
3326
3327 case CALL(path=IDENT(name="Integer"), expLst={toBeCasted}) then
3328 let castedVar = daeExpXml(toBeCasted, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3329 '<%castedVar%>'
3330
3331 case CALL(path=IDENT(name="clock"), expLst={}) then
3332 'mmc_clock()'
3333
3334 case CALL(path=IDENT(name="noEvent"), expLst={e1}) then
3335 daeExpXml(e1, context, &preExp, &varDecls)
3336
3337 case CALL(path=IDENT(name="anyString"), expLst={e1}) then
3338 '<%daeExpXml(e1, context, &preExp, &varDecls)%>'
3339
3340 case CALL(path=IDENT(name="mmc_get_field"), expLst={s1, ICONST(integer=i)}) then
3341 let tvar = tempDeclXml("modelica_metatype", &varDecls /*BUFD*/)
3342 let expPart = daeExpXml(s1, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3343 let &preExp += '<%tvar%> = MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(<%expPart%>), <%i%>));<%\n%>'
3344 '<%tvar%>'
3345
3346 case CALL(path=IDENT(name = "mmc_unbox_record"), expLst={s1}, attr=CALL_ATTR(ty=ty)) then
3347 <<
3348 "mmc_unbox_record" is not necessary
3349 >>
3350
3351 case exp as CALL(attr=attr as CALL_ATTR(tailCall=tail as TAIL(__))) then
3352 let res = <<
3353 /* Tail recursive call <%ExpressionDumpTpl.dumpExp(exp,"\"")%> */
3354 <%daeExpTailCallXml(expLst,tail.vars,context,&preExp,&varDecls)%>goto _tailrecursive;
3355 /* TODO: Make sure any eventual dead code below is never generated */
3356 >>
3357 let &preExp += res
3358 ""
3359
3360 case exp as CALL(attr=attr as CALL_ATTR(__)) then
3361 let &preExp = buffer "" /*BUFD*/
3362 let argStr = (expLst |> exp => '<%daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>' ;separator="\n")
3363 let result = if preExp then preExp else argStr
3364 //let builtinName ='<%dotPathXml(path)%>'
3365 let builtinFunctionName ='<%builtinFunctionNameXml(path)%>'
3366 let funName = '<%underscorePathXml(path)%>'
3367 let retType = if attr.builtin then (match attr.ty case T_NORETCALL(__) then ""
3368 else expTypeModelicaXml(attr.ty))
3369 else '<%funName%>'
3370 let retVar = match attr.ty
3371 case T_NORETCALL(__) then ""
3372 else tempDeclXml(retType, &varDecls)
3373 match exp
3374 // no return calls
3375 case CALL(attr=CALL_ATTR(ty=T_NORETCALL(__))) then '/* NORETCALL */'
3376 // non tuple calls (single return value)
3377 case CALL(attr=CALL_ATTR(tuple_=false)) then
3378 if attr.builtin then
3379 <<
3380 <exp:<%builtinFunctionName%>>
3381 <%result%>
3382 </exp:<%builtinFunctionName%>>
3383 >>
3384 else
3385 <<
3386 <exp:FunctionCall>
3387 <exp:Name>
3388 <%funName%>
3389 </exp:Name>
3390 <exp:Arguments>
3391 <%result%>
3392 </exp:Arguments>
3393 </exp:FunctionCall>
3394 >>
3395 // tuple calls (multiple return values)
3396 else
3397 <<
3398 <exp:FunctionCall>
3399 <exp:Name>
3400 <%funName%>
3401 </exp:Name>
3402 <exp:Arguments>
3403 <%result%>
3404 </exp:Arguments>
3405 </exp:FunctionCall>
3406 >>
3407 end daeExpCallXml;
3408
3409 3 template builtinFunctionNameXml(Absyn.Path path)
3410 ::=
3411 match path
3412 case IDENT(name="DIVISION") then 'Div'
3413 case IDENT(name="ADDITION") then 'Add'
3414 case IDENT(name="SUBTRACTION") then 'Sub'
3415 case IDENT(name="POWER") then 'Pow'
3416 case IDENT(name="sin") then 'Sin'
3417 case IDENT(name="cos") then 'Cos'
3418 case IDENT(name="tan") then 'Tan'
3419 case IDENT(name="asin") then 'Asin'
3420 case IDENT(name="acos") then 'Acos'
3421 case IDENT(name="atan") then 'Atan'
3422 case IDENT(name="sinh") then 'Sinh'
3423 case IDENT(name="cosh") then 'Cosh'
3424 case IDENT(name="tanh") then 'Tanh'
3425 case IDENT(name="exp") then 'Exp'
3426 case IDENT(name="log") then 'Log'
3427 case IDENT(name="log10") then 'Log10'
3428 case IDENT(name="sqrt") then 'Sqrt'
3429 case IDENT(name="atan2") then 'Atan2'
3430 case IDENT(name="abs") then 'Abs'
3431 case IDENT(name="sign") then 'Sign'
3432 case IDENT(name="min") then 'Min'
3433 case IDENT(name="max") then 'Max'
3434 case IDENT(name="noEvent") then 'NoEvent'
3435 case IDENT(name="array") then 'Array'
3436 case IDENT(name="sample") then 'Sample'
3437 case IDENT(name="smooth") then 'Smooth'
3438 case IDENT(name="homotopy") then 'Homotopy'
3439 else '<%dotPathXml(path)%>'
3440 end builtinFunctionNameXml;
3441
3442 ✗ template daeExpTailCallXml(list<DAE.Exp> es, list<String> vs, Context context, Text &preExp, Text &varDecls)
3443 ::=
3444 match es
3445 case e::erest then
3446 match vs
3447 case v::vrest then
3448 let exp = daeExpXml(e,context,&preExp,&varDecls)
3449 match e
3450 case CREF(componentRef = cr, ty = T_FUNCTION_REFERENCE_VAR(__)) then
3451 // adrpo: ignore _x = _x!
3452 if stringEq(v, crefStrXml(cr))
3453 then '<%daeExpTailCallXml(erest, vrest, context, &preExp, &varDecls)%>'
3454 else '_<%v%> = <%exp%>;<%\n%><%daeExpTailCallXml(erest, vrest, context, &preExp, &varDecls)%>'
3455 case _ then
3456 '_<%v%> = <%exp%>;<%\n%><%daeExpTailCallXml(erest, vrest, context, &preExp, &varDecls)%>'
3457 end daeExpTailCallXml;
3458
3459 ✗ template daeExpCallBuiltinPrefixXml(Boolean builtin)
3460 "Helper to daeExpCallXml."
3461 ::=
3462 match builtin
3463 case true then ""
3464 case false then "_"
3465 end daeExpCallBuiltinPrefixXml;
3466
3467 ✗ template daeExpArrayXml(Exp exp, Context context, Text &preExp /*BUFP*/,
3468 Text &varDecls /*BUFP*/)
3469 "Generates code for an array expression."
3470 ::=
3471 match exp
3472 case ARRAY(__) then
3473 let params = (array |> e =>
3474 '<%daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)%>'
3475 ;separator="\n")
3476 let &preExp +=
3477 <<
3478 <exp:Array>
3479 <%params%>
3480 </exp:Array>
3481 >>
3482 params
3483 end daeExpArrayXml;
3484
3485 ✗ template daeExpMatrixXml(Exp exp, Context context, Text &preExp /*BUFP*/,
3486 Text &varDecls /*BUFP*/)
3487 "Generates code for a matrix expression."
3488 ::=
3489 match exp
3490 case MATRIX(matrix={{}}) // special case for empty matrix: create dimensional array Real[0,1]
3491 case MATRIX(matrix={}) // special case for empty array: create dimensional array Real[0,1]
3492 then ''
3493 case m as MATRIX(__) then
3494 let arrayTypeStr = expTypeArrayXml(m.ty)
3495 let &vars2 = buffer "" /*BUFD*/
3496 let &promote = buffer "" /*BUFD*/
3497 let catAlloc = (m.matrix |> row =>
3498 let tmp = tempDeclXml(arrayTypeStr, &varDecls /*BUFD*/)
3499 let vars = daeExpMatrixRowXml(row, arrayTypeStr, context,
3500 &promote /*BUFC*/, &varDecls /*BUFD*/)
3501 let &vars2 += ', &<%tmp%>'
3502 '';separator="\n")
3503 let &preExp += promote
3504 let &preExp += catAlloc
3505 let &preExp += "\n"
3506 let tmp = tempDeclXml(arrayTypeStr, &varDecls /*BUFD*/)
3507 let &preExp += ''
3508 tmp
3509 end daeExpMatrixXml;
3510
3511 ✗ template daeExpMatrixRowXml(list<Exp> row, String arrayTypeStr,
3512 Context context, Text &preExp /*BUFP*/,
3513 Text &varDecls /*BUFP*/)
3514 "Helper to daeExpMatrixXML."
3515 ::=
3516 let &varLstStr = buffer "" /*BUFD*/
3517
3518 let preExp2 = (row |> e =>
3519 let expVar = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3520 let tmp = tempDeclXml(arrayTypeStr, &varDecls /*BUFD*/)
3521 let &varLstStr += ', &<%tmp%>'
3522 <<
3523 <%expVar%>
3524 >>
3525 ;separator="\n")
3526 let &preExp2 += "\n"
3527 let &preExp += preExp2
3528 varLstStr
3529 end daeExpMatrixRowXml;
3530
3531 ✗ template daeExpRangeXml(Exp exp, Context context, Text &preExp /*BUFP*/,
3532 Text &varDecls /*BUFP*/)
3533 "Generates XML code for a range expression."
3534 ::=
3535 match exp
3536 case RANGE(__) then
3537 let ty_str = expTypeArrayXml(ty)
3538 let start_exp = daeExpXml(start, context, &preExp, &varDecls)
3539 let stop_exp = daeExpXml(stop, context, &preExp, &varDecls)
3540 let tmp = tempDeclXml(ty_str, &varDecls)
3541 let step_exp = match step case SOME(stepExp) then daeExpXml(stepExp, context, &preExp, &varDecls) else "1"
3542 let &preExp +=
3543 <<
3544 <exp:Range>
3545 <%start_exp%>
3546 <%step_exp%>
3547 <%stop_exp%>
3548 </exp:Range><%\n%>
3549 >>
3550 '<%tmp%>'
3551 end daeExpRangeXml;
3552
3553 ✗ template daeExpCastXml(Exp exp, Context context, Text &preExp /*BUFP*/,
3554 Text &varDecls /*BUFP*/)
3555 "Generates code for a cast expression."
3556 ::=
3557 match exp
3558 case CAST(__) then
3559 let expVar = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3560 match ty
3561 case T_INTEGER(__) then '<%expVar%>'
3562 case T_REAL(__) then '<%expVar%>'
3563 case T_ENUMERATION(__) then '<%expVar%>'
3564 case T_BOOL(__) then '<%expVar%>'
3565 case T_ARRAY(__) then
3566 let arrayTypeStr = expTypeArrayXml(ty)
3567 let tvar = tempDeclXml(arrayTypeStr, &varDecls /*BUFD*/)
3568 let to = expTypeShortXml(ty)
3569 let from = expTypeFromExpShortXml(exp)
3570 let &preExp += 'cast_<%from%>_array_to_<%to%>(&<%expVar%>, &<%tvar%>);<%\n%>'
3571 '<%tvar%>'
3572 else
3573 '<%expVar%> /* could not cast, using the variable as it is */'
3574 end daeExpCastXml;
3575
3576 ✗ template daeSubscriptXML(DAE.Subscript sub, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
3577 ::=
3578 match sub
3579 case sub as INDEX() then daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3580 else error(sourceInfo(), 'non INDEX(_) (i.e., slice) subscripts probably should not reach here. Check indexedAssign template.')
3581 end match
3582 end daeSubscriptXML;
3583
3584 ✗ template daeExpAsubXml(Exp inExp, Context context, Text &preExp /*BUFP*/,
3585 Text &varDecls /*BUFP*/)
3586 "Generates XML code for an asub expression."
3587 ::=
3588 match expTypeFromExpShortXml(inExp)
3589 case "metatype" then
3590 // MetaModelica Array
3591 (match inExp case ASUB(exp=e, sub={idx}) then
3592 let e1 = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3593 let idx1 = daeSubscriptXML(idx, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3594 'arrayGet(<%e1%>,<%idx1%>) /* DAE.ASUB */')
3595 // Modelica Array
3596 else
3597 match inExp
3598
3599 case ASUB(exp=ASUB(__)) then
3600 error(sourceInfo(),'Nested array subscripting *should* have been handled by the routine creating the asub, but for some reason it was not: <%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
3601
3602 // Faster asub: Do not construct a whole new array just to access one subscript
3603 case ASUB(exp=exp as ARRAY(scalar=true), sub={idx}) then
3604 let res = tempDeclXml(expTypeFromExpModelicaXml(exp),&varDecls) +' asub tmp test'
3605 let idx1 = daeSubscriptXML(idx, context, &preExp, &varDecls)
3606 let expl = (exp.array |> e hasindex i1 fromindex 1 =>
3607 let &caseVarDecls = buffer ""
3608 let &casePreExp = buffer ""
3609 let v = daeExpXml(e, context, &casePreExp, &caseVarDecls)
3610 <<
3611 case <%i1%>: {
3612 <%&caseVarDecls%>
3613 <%&casePreExp%>
3614 <%res%> = <%v%>;
3615 break;
3616 }
3617 >> ; separator = "\n")
3618 let &preExp +=
3619 <<
3620 switch (<%idx1%>) { /* ASUB */
3621 <%expl%>
3622 default:
3623 assert(NULL == "index out of bounds");
3624 }
3625 >>
3626 res
3627
3628 case ASUB(exp=RANGE(ty=t), sub={idx}) then
3629 error(sourceInfo(),'ASUB_EASY_CASE <%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
3630
3631 case ASUB(exp=ecr as CREF(__), sub=subs) then
3632 let arrName = daeExpCrefRhsXml(buildCrefExpFromSubs(ecr, subs), context,
3633 &preExp /*BUFC*/, &varDecls /*BUFD*/)
3634 match context case FUNCTION_CONTEXT(__) then
3635 arrName
3636 else
3637 arrayScalarRhsXml(ecr.ty, subs, arrName, context, &preExp, &varDecls) + 'Asub array scalar RHS'
3638
3639 case ASUB(exp=e, sub=indexes) then
3640 let exp = daeExpXml(e, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3641 '<%exp%>'
3642
3643 case exp then
3644 error(sourceInfo(),'OTHER_ASUB <%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
3645 end daeExpAsubXml;
3646
3647 ✗ template daeExpASubIndexXml(Exp exp, Context context, Text &preExp, Text &varDecls)
3648 ::=
3649 match exp
3650 case ICONST(__) then incrementInt(integer,-1)
3651 case ENUM_LITERAL(__) then incrementInt(index,-1)
3652 else daeExpXml(exp,context,&preExp,&varDecls)
3653 end daeExpASubIndexXml;
3654
3655 1 template daeExpCallPreXml(Exp exp, Context context, Text &preExp /*BUFP*/,
3656 Text &varDecls /*BUFP*/)
3657 "Generates code for an asub of a cref, which becomes cref + offset."
3658 ::=
3659 match exp
3660 case cr as CREF(__) then
3661 <<
3662 <exp:Pre>
3663 <%crefXml(cr.componentRef)%>
3664 </exp:Pre>
3665 >>
3666 case ASUB(exp = cr as CREF(__), sub = {sub_exp}) then
3667 <<
3668 "case ASUB(exp = cr as CREF(__), sub = {sub_exp}) is not yet implemented"
3669 >>
3670 else
3671 error(sourceInfo(), 'Code generation does not support pre(<%ExpressionDumpTpl.dumpExp(exp,"\"")%>)')
3672 end daeExpCallPreXml;
3673
3674 ✗ template daeExpSizeXml(Exp exp, Context context, Text &preExp /*BUFP*/,
3675 Text &varDecls /*BUFP*/)
3676 "Generates XML code for a size expression."
3677 ::=
3678 match exp
3679 case SIZE(exp=CREF(__), sz=SOME(dim)) then
3680 let expPart = daeExpXml(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3681 let dimPart = daeExpXml(dim, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3682 <<
3683 <exp:Size>
3684 <%expPart%>
3685 <%dimPart%>
3686 </exp:Size>
3687 >>
3688 else "size(X) not implemented"
3689 end daeExpSizeXml;
3690
3691 ✗ template daeExpBoxXml(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
3692 "Generates XML code for a match expression box."
3693 ::=
3694 match exp
3695 case exp as BOX(__) then
3696 let res = daeExpXml(exp.exp,context,&preExp,&varDecls)
3697 <<
3698 <%res%>
3699 >>
3700 end daeExpBoxXml;
3701
3702 ✗ template daeExpUnboxXml(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
3703 "Generates XML code for a match expression unbox."
3704 ::=
3705 match exp
3706 case exp as UNBOX(__) then
3707 let res = daeExpXml(exp.exp,context,&preExp,&varDecls)
3708 <<
3709 <%res%>
3710 >>
3711 end daeExpUnboxXml;
3712
3713 4 template daeExpSharedLiteralXml(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
3714 ::=
3715 match exp case exp as SHARED_LITERAL(__) then daeExpXml(exp.exp, context, &preExp, &varDecls)
3716 end daeExpSharedLiteralXml;
3717
3718 // TODO: Optimize as in Codegen
3719 // TODO: Use this function in other places where almost the same thing is hard
3720 // coded
3721 ✗ template arrayScalarRhsXml(Type ty, list<DAE.Subscript> subs, String arrName, Context context,
3722 Text &preExp /*BUFP*/, Text &varDecls /*BUFP*/)
3723 "Helper to daeExpAsub."
3724 ::=
3725 let arrayType = expTypeArrayXml(ty)
3726 let dimsLenStr = listLength(subs)
3727 let dimsValuesStr = (subs |> sub =>
3728 daeSubscriptXML(sub, context, &preExp /*BUFC*/, &varDecls /*BUFD*/)
3729
3730 ;separator=", ")
3731 match arrayType
3732 case "metatype_array" then
3733 'arrayGet(<%arrName%>,<%dimsValuesStr%>) /*arrayScalarRhs*/'
3734 else
3735 << wrong LHS
3736 <exp:ArraySubscripts>
3737 <exp:IndexExpression>
3738 <%dimsValuesStr%>
3739 </exp:IndexExpression>
3740 </exp:ArraySubscripts>
3741 </exp:QualifiedNamepart>
3742 </exp:QualifiedName>
3743 >>
3744 end arrayScalarRhsXml;
3745
3746
3747 /*****************************************************************************
3748 * SECTION:
3749 *****************************************************************************/
3750
3751 ✗ template outDeclXml(String ty, Text &varDecls /*BUFP*/)
3752 "Declares a temporary variable in varDecls and returns the name."
3753 ::=
3754 let newVar = 'out'
3755 let &varDecls += '<%ty%> <%newVar%>;<%\n%>'
3756 newVar
3757 end outDeclXml;
3758
3759 20 template tempDeclXml(String ty, Text &varDecls /*BUFP*/)
3760 "Declares a temporary variable in varDecls and returns the name."
3761 ::=
3762 let newVar
3763 =
3764 match ty /* TODO! FIXME! UGLY! UGLY! hack! */
3765 case "modelica_metatype"
3766 case "metamodelica_string"
3767 case "metamodelica_string_const"
3768 then 'tmpMeta[<%System.tmpTickIndex(1)%>]'
3769 else
3770 let newVarIx = 'tmp<%System.tmpTick()%>'
3771 let &varDecls += '<%ty%> <%newVarIx%>;<%\n%>'
3772 newVarIx
3773 newVar
3774 end tempDeclXml;
3775
3776 ✗ template tempDeclConstXml(String ty, String val, Text &varDecls /*BUFP*/)
3777 "Declares a temporary variable in varDecls and returns the name."
3778 ::=
3779 let newVar = 'tmp<%System.tmpTick()%>'
3780 let &varDecls += '<%ty%> <%newVar%> = <%val%>;<%\n%>'
3781 newVar
3782 end tempDeclConstXml;
3783
3784 4 template varTypeXml(Variable var)
3785 "Generates type for a variable."
3786 ::=
3787 match var
3788 case var as VARIABLE(__) then
3789 if instDims then
3790 expTypeArrayXml(var.ty)
3791 else
3792 expTypeArrayIfXml(var.ty)
3793 end varTypeXml;
3794
3795 ✗ template varTypeBoxedXml(Variable var)
3796 ::=
3797 match var
3798 case VARIABLE(__) then 'modelica_metatype'
3799 case FUNCTION_PTR(__) then 'modelica_fnptr'
3800 end varTypeBoxedXml;
3801
3802 ✗ template expTypeRWXml(DAE.Type type)
3803 "Helper to writeOutVarRecordMembers."
3804 ::=
3805 match type
3806 case T_INTEGER(__) then "TYPE_DESC_INT"
3807 case T_REAL(__) then "TYPE_DESC_REAL"
3808 case T_STRING(__) then "TYPE_DESC_STRING"
3809 case T_BOOL(__) then "TYPE_DESC_BOOL"
3810 case T_ENUMERATION(__) then "TYPE_DESC_INT"
3811 case T_ARRAY(__) then '<%expTypeRWXml(ty)%>_ARRAY'
3812 case T_COMPLEX(complexClassType=RECORD(__))
3813 then "TYPE_DESC_RECORD"
3814 case T_METATYPE(__) case T_METABOXED(__) then "TYPE_DESC_MMC"
3815 end expTypeRWXml;
3816
3817 7 template expTypeShortXml(DAE.Type type)
3818 "Generate type helper."
3819 ::=
3820 match type
3821 case T_INTEGER(__) then "Integer"
3822 case T_REAL(__) then "Real"
3823 case T_STRING(__) then if acceptMetaModelicaGrammar() then "MetaType" else "String"
3824 case T_BOOL(__) then "Boolean"
3825 case T_ENUMERATION(__) then "Integer"
3826 case T_ARRAY(__) then expTypeShortXml(ty)
3827 case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__))
3828 then "Complex"
3829 case T_COMPLEX(__) then '<%underscorePathXml(ClassInfUtil.getStateName(complexClassType))%>'
3830 case T_METATYPE(__) case T_METABOXED(__) then "MetaType"
3831 case T_FUNCTION_REFERENCE_VAR(__) then "fnptr"
3832 case T_UNKNOWN(__) then "Complex" /* TODO: Don't do this to me! */
3833 case T_ANYTYPE(__) then "Complex" /* TODO: Don't do this to me! */
3834 else error(sourceInfo(),'expTypeShortXml:<%unparseType(type)%>')
3835 end expTypeShortXml;
3836
3837 ✗ template expTypeXml(DAE.Type ty, Boolean array)
3838 "Generate type helper."
3839 ::=
3840 match array
3841 case true then expTypeArrayXml(ty)
3842 case false then expTypeModelicaXml(ty)
3843 end expTypeXml;
3844
3845 5 template expTypeModelicaXml(DAE.Type ty)
3846 "Generate type helper."
3847 ::=
3848 expTypeFlagXml(ty, 2)
3849 end expTypeModelicaXml;
3850
3851 ✗ template expTypeArrayXml(DAE.Type ty)
3852 "Generate type helper."
3853 ::=
3854 expTypeFlagXml(ty, 3)
3855 end expTypeArrayXml;
3856
3857 4 template expTypeArrayIfXml(DAE.Type ty)
3858 "Generate type helper."
3859 ::=
3860 expTypeFlagXml(ty, 4)
3861 end expTypeArrayIfXml;
3862
3863 ✗ template expTypeFromExpShortXml(Exp exp)
3864 "Generate type helper."
3865 ::=
3866 expTypeFromExpFlagXml(exp, 1)
3867 end expTypeFromExpShortXml;
3868
3869 ✗ template expTypeFromExpModelicaXml(Exp exp)
3870 "Generate type helper."
3871 ::=
3872 expTypeFromExpFlagXml(exp, 2)
3873 end expTypeFromExpModelicaXml;
3874
3875 ✗ template expTypeFromExpArrayXml(Exp exp)
3876 "Generate type helper."
3877 ::=
3878 expTypeFromExpFlagXml(exp, 3)
3879 end expTypeFromExpArrayXml;
3880
3881 ✗ template expTypeFromExpArrayIfXml(Exp exp)
3882 "Generate type helper."
3883 ::=
3884 expTypeFromExpFlagXml(exp, 4)
3885 end expTypeFromExpArrayIfXml;
3886
3887 13 template expTypeFlagXml(DAE.Type ty, Integer flag)
3888 "Generate type helper."
3889 ::=
3890 match flag
3891 case 1 then
3892 // we want the short type
3893 expTypeShortXml(ty)
3894 case 2 then
3895 // we want the "modelica type"
3896 match ty case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then
3897 '<%expTypeShortXml(ty)%>'
3898 else match ty case T_COMPLEX(__) then '<%underscorePathXml(ClassInfUtil.getStateName(complexClassType))%>'
3899 else
3900 '<%expTypeShortXml(ty)%>'
3901 case 3 then
3902 // we want the "array type"
3903 '<%expTypeShortXml(ty)%>'
3904 case 4 then
3905 // we want the "array type" only if type is array, otherwise "modelica type"
3906 match ty
3907 case T_ARRAY(__) then '<%expTypeShortXml(ty)%>'
3908 else expTypeFlagXml(ty, 2)
3909 end expTypeFlagXml;
3910
3911 ✗ template expTypeFromExpFlagXml(Exp exp, Integer flag)
3912 "Generate type helper."
3913 ::=
3914 match exp
3915 case ICONST(__) then match flag case 8 then "int" case 1 then "integer" else "modelica_integer"
3916 case RCONST(__) then match flag case 1 then "real" else "modelica_real"
3917 case SCONST(__) then if acceptMetaModelicaGrammar() then
3918 (match flag case 1 then "metatype" else "modelica_metatype")
3919 else
3920 (match flag case 1 then "string" else "modelica_string")
3921 case BCONST(__) then match flag case 1 then "boolean" else "modelica_boolean"
3922 case ENUM_LITERAL(__) then match flag case 8 then "int" case 1 then "integer" else "modelica_integer"
3923 case e as BINARY(__)
3924 case e as UNARY(__)
3925 case e as LBINARY(__)
3926 case e as LUNARY(__)
3927 case e as RELATION(__) then expTypeFromOpFlagXml(e.operator, flag)
3928 case IFEXP(__) then expTypeFromExpFlagXml(expThen, flag)
3929 case CALL(attr=CALL_ATTR(__)) then expTypeFlagXml(attr.ty, flag)
3930 case c as ARRAY(__)
3931 case c as MATRIX(__)
3932 case c as RANGE(__)
3933 case c as CAST(__)
3934 case c as CREF(__)
3935 case c as CODE(__) then expTypeFlagXml(c.ty, flag)
3936 case c as ASUB(__) then expTypeFlagXml(typeof(c), flag)
3937 case REDUCTION(__) then expTypeFlagXml(typeof(exp), flag)
3938 case e as BOX(__)
3939 case e as CONS(__)
3940 case e as LIST(__)
3941 case e as SIZE(__) then expTypeFlagXml(typeof(e), flag)
3942
3943 case META_TUPLE(__)
3944 case META_OPTION(__)
3945 case MATCHEXPRESSION(__)
3946 case METARECORDCALL(__)
3947 case BOX(__) then match flag case 1 then "metatype" else "modelica_metatype"
3948 case c as UNBOX(__) then expTypeFlagXml(c.ty, flag)
3949 case c as SHARED_LITERAL(__) then expTypeFromExpFlagXml(c.exp, flag)
3950 else error(sourceInfo(), 'expTypeFromExpFlag:<%ExpressionDumpTpl.dumpExp(exp,"\"")%>')
3951 end expTypeFromExpFlagXml;
3952
3953 ✗ template expTypeFromOpFlagXml(Operator op, Integer flag)
3954 "Generate type helper."
3955 ::=
3956 match op
3957 case o as ADD(__)
3958 case o as SUB(__)
3959 case o as MUL(__)
3960 case o as DIV(__)
3961 case o as POW(__)
3962
3963 case o as UMINUS(__)
3964 case o as UMINUS_ARR(__)
3965 case o as ADD_ARR(__)
3966 case o as SUB_ARR(__)
3967 case o as MUL_ARR(__)
3968 case o as DIV_ARR(__)
3969 case o as MUL_ARRAY_SCALAR(__)
3970 case o as ADD_ARRAY_SCALAR(__)
3971 case o as SUB_SCALAR_ARRAY(__)
3972 case o as MUL_SCALAR_PRODUCT(__)
3973 case o as MUL_MATRIX_PRODUCT(__)
3974 case o as DIV_ARRAY_SCALAR(__)
3975 case o as DIV_SCALAR_ARRAY(__)
3976 case o as POW_ARRAY_SCALAR(__)
3977 case o as POW_SCALAR_ARRAY(__)
3978 case o as POW_ARR(__)
3979 case o as POW_ARR2(__)
3980 case o as LESS(__)
3981 case o as LESSEQ(__)
3982 case o as GREATER(__)
3983 case o as GREATEREQ(__)
3984 case o as EQUAL(__)
3985 case o as NEQUAL(__) then
3986 expTypeFlagXml(o.ty, flag)
3987 case o as AND(__)
3988 case o as OR(__)
3989 case o as NOT(__) then
3990 match flag case 1 then "boolean" else "modelica_boolean"
3991 else "expTypeFromOpFlag:ERROR"
3992 end expTypeFromOpFlagXml;
3993
3994 ✗ template dimensionXml(Dimension d)
3995 ::=
3996 match d
3997 case DAE.DIM_INTEGER(__) then integer
3998 case DAE.DIM_ENUM(__) then size
3999 case DAE.DIM_UNKNOWN(__) then ":"
4000 else "INVALID_DIMENSION"
4001 end dimensionXml;
4002
4003 3 template assertCommonXml(Exp condition, Exp message, Context context, Text &varDecls, builtin.SourceInfo info)
4004 ::=
4005 let &preExpCond = buffer ""
4006 let &preExpMsg = buffer ""
4007 let condVar = daeExpXml(condition, context, &preExpCond, &varDecls)
4008 let msgVar = daeExpXml(message, context, &preExpMsg, &varDecls)
4009 <<
4010 <fun:Assertion>
4011 <fun:Condition>
4012 <%condVar%>
4013 </fun:Condition>
4014 <fun:Message>
4015 <%msgVar%>
4016 </fun:Message>
4017 </fun:Assertion>
4018 >>
4019 end assertCommonXml;
4020
4021 ✗ template error(builtin.SourceInfo srcInfo, String errMessage)
4022 "Example source template error reporting template to be used together with the sourceInfo() magic function.
4023 Usage: error(sourceInfo(), <<message>>) "
4024 ::=
4025 let() = Tpl.addSourceTemplateError(errMessage, srcInfo)
4026 <<
4027
4028 #error "<% Error.infoStr(srcInfo) %> <% errMessage %>"<%\n%>
4029 >>
4030 end error;
4031
4032 annotation(__OpenModelica_Interface="codegen_xml");
4033 end CodegenXML;
4034 // vim: filetype=susan sw=2 sts=2
4035