Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 43.3% 45 / 0 / 104
Functions: -% 0 / 1 / 1
Branches: 45.1% 55 / 0 / 122

OMCompiler/Compiler/NFFrontEnd/NFSections.mo
Line Branch Exec Source
1 /*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36 encapsulated uniontype NFSections
37 import BaseModelica;
38 import Equation = NFEquation;
39 import Algorithm = NFAlgorithm;
40 import Statement = NFStatement;
41 import ComponentRef = NFComponentRef;
42 import Expression = NFExpression;
43 import SCode;
44
45 protected
46 import Absyn;
47 import DAEDumpTypes;
48 import Sections = NFSections;
49 import SCodeUtil;
50 import IOStream;
51
52 public
53 record SECTIONS
54 list<Equation> equations;
55 list<Equation> initialEquations;
56 list<Algorithm> algorithms;
57 list<Algorithm> initialAlgorithms;
58 end SECTIONS;
59
60 record EXTERNAL
61 String name;
62 list<Expression> args;
63 ComponentRef outputRef;
64 String language;
65 Option<SCode.Annotation> ann;
66 Boolean explicit;
67 SourceInfo info;
68 end EXTERNAL;
69
70 record EMPTY end EMPTY;
71
72 function new
73 input list<Equation> equations;
74 input list<Equation> initialEquations;
75 input list<Algorithm> algorithms;
76 input list<Algorithm> initialAlgorithms;
77 output Sections sections;
78 algorithm
79
7/8
✓ Branch 0 taken 233310 times.
✓ Branch 1 taken 13993 times.
✓ Branch 2 taken 233232 times.
✓ Branch 3 taken 78 times.
✓ Branch 4 taken 216364 times.
✓ Branch 5 taken 16868 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 216364 times.
247303 if listEmpty(equations) and listEmpty(initialEquations) and
80 listEmpty(algorithms) and listEmpty(initialAlgorithms) then
81 sections := EMPTY();
82 else
83 30939 sections := SECTIONS(equations, initialEquations, algorithms, initialAlgorithms);
84 end if;
85 end new;
86
87 function equations
88 input Sections sections;
89 output list<Equation> equations;
90 algorithm
91 equations := match sections
92 4 case SECTIONS() then sections.equations;
93 else {};
94 end match;
95 end equations;
96
97 function prepend
98 input list<Equation> equations;
99 input list<Equation> initialEquations;
100 input list<Algorithm> algorithms;
101 input list<Algorithm> initialAlgorithms;
102 input output Sections sections;
103 algorithm
104 sections := match sections
105 case SECTIONS()
106 8933 then SECTIONS(
107 listAppend(equations, sections.equations),
108 listAppend(initialEquations, sections.initialEquations),
109 listAppend(algorithms, sections.algorithms),
110 listAppend(initialAlgorithms, sections.initialAlgorithms));
111
112 897 else SECTIONS(equations, initialEquations, algorithms, initialAlgorithms);
113 end match;
114 end prepend;
115
116 function prependEquation
117 input Equation eq;
118 input output Sections sections;
119 input Boolean isInitial = false;
120 algorithm
121 sections := match sections
122 case SECTIONS()
123 algorithm
124
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 18392 times.
18444 if isInitial then
125 104 sections.initialEquations := eq :: sections.initialEquations;
126 else
127 36784 sections.equations := eq :: sections.equations;
128 end if;
129 then
130 sections;
131
132 case EMPTY()
133
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 181 times.
366 then if isInitial then SECTIONS({}, {eq}, {}, {}) else SECTIONS({eq}, {}, {}, {});
134
135 else
136 algorithm
137 ✗ Error.terminate(getInstanceName() +
138 " got invalid Sections to prepend equation to", sourceInfo());
139 ✗ then
140 fail();
141
142 end match;
143 end prependEquation;
144
145 function prependAlgorithm
146 input Algorithm alg;
147 input output Sections sections;
148 input Boolean isInitial = false;
149 algorithm
150 sections := match sections
151 case SECTIONS()
152 algorithm
153 ✗ if isInitial then
154 ✗ sections.initialAlgorithms := alg :: sections.initialAlgorithms;
155 else
156 ✗ sections.algorithms := alg :: sections.algorithms;
157 end if;
158 then
159 sections;
160
161 case EMPTY()
162 ✗ then if isInitial then SECTIONS({}, {}, {}, {alg}) else SECTIONS({}, {}, {alg}, {});
163
164 else
165 algorithm
166 ✗ Error.terminate(getInstanceName() +
167 " got invalid Sections to prepend algorithm to", sourceInfo());
168 ✗ then
169 fail();
170
171 end match;
172 end prependAlgorithm;
173
174 function append
175 input list<Equation> equations;
176 input list<Equation> initialEquations;
177 input list<Algorithm> algorithms;
178 input list<Algorithm> initialAlgorithms;
179 input output Sections sections;
180 algorithm
181 sections := match sections
182 case SECTIONS()
183 ✗ then SECTIONS(
184 listAppend(sections.equations, equations),
185 listAppend(sections.initialEquations, initialEquations),
186 listAppend(sections.algorithms, algorithms),
187 listAppend(sections.initialAlgorithms, initialAlgorithms));
188
189 ✗ else SECTIONS(equations, initialEquations, algorithms, initialAlgorithms);
190 end match;
191 end append;
192
193 function join
194 input Sections sections1;
195 input Sections sections2;
196 output Sections sections;
197 algorithm
198 sections := match (sections1, sections2)
199 case (EMPTY(), _) then sections2;
200 case (_, EMPTY()) then sections1;
201
202 case (SECTIONS(), SECTIONS())
203 4752 then SECTIONS(
204 listAppend(sections1.equations, sections2.equations),
205 listAppend(sections1.initialEquations, sections2.initialEquations),
206 listAppend(sections1.algorithms, sections2.algorithms),
207 listAppend(sections1.initialAlgorithms, sections2.initialAlgorithms));
208
209 end match;
210 end join;
211
212 function map
213 input output Sections sections;
214 input EquationFn eqFn = eqId;
215 input AlgorithmFn algFn = algId;
216 input EquationFn ieqFn = eqFn;
217 input AlgorithmFn ialgFn = algFn;
218
219 partial function EquationFn
220 input output Equation eq;
221 end EquationFn;
222
223 partial function AlgorithmFn
224 input output Algorithm alg;
225 end AlgorithmFn;
226
227 function eqId
228 input output Equation eq;
229 end eqId;
230
231 function algId
232 input output Algorithm alg;
233 end algId;
234 protected
235 list<Equation> eq, ieq;
236 list<Algorithm> alg, ialg;
237 algorithm
238 () := match sections
239 case SECTIONS()
240 algorithm
241
5/6
✓ Branch 0 taken 56305 times.
✓ Branch 1 taken 18135 times.
✓ Branch 2 taken 56305 times.
✓ Branch 3 taken 18135 times.
✓ Branch 4 taken 56305 times.
✗ Branch 5 not taken.
74440 eq := list(eqFn(e) for e in sections.equations);
242
5/6
✓ Branch 0 taken 1571 times.
✓ Branch 1 taken 18135 times.
✓ Branch 2 taken 1571 times.
✓ Branch 3 taken 18135 times.
✓ Branch 4 taken 1571 times.
✗ Branch 5 not taken.
19706 ieq := list(ieqFn(e) for e in sections.initialEquations);
243
6/6
✓ Branch 0 taken 9270 times.
✓ Branch 1 taken 18132 times.
✓ Branch 2 taken 9270 times.
✓ Branch 3 taken 18132 times.
✓ Branch 4 taken 145 times.
✓ Branch 5 taken 9125 times.
27402 alg := list(algFn(a) for a in sections.algorithms);
244
5/6
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 18132 times.
✓ Branch 2 taken 52 times.
✓ Branch 3 taken 18132 times.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
18184 ialg := list(ialgFn(a) for a in sections.initialAlgorithms);
245 18132 sections := SECTIONS(eq, ieq, alg, ialg);
246 then
247 ();
248
249 else ();
250 end match;
251 end map;
252
253 function map1<ArgT>
254 input output Sections sections;
255 input ArgT arg;
256 input EquationFn eqFn;
257 input AlgorithmFn algFn;
258 input EquationFn ieqFn = eqFn;
259 input AlgorithmFn ialgFn = algFn;
260
261 partial function EquationFn
262 input output Equation eq;
263 input ArgT arg;
264 end EquationFn;
265
266 partial function AlgorithmFn
267 input output Algorithm alg;
268 input ArgT arg;
269 end AlgorithmFn;
270 protected
271 list<Equation> eq, ieq;
272 list<Algorithm> alg, ialg;
273 algorithm
274 () := match sections
275 case SECTIONS()
276 algorithm
277 ✗ eq := list(eqFn(e, arg) for e in sections.equations);
278 ✗ ieq := list(ieqFn(e, arg) for e in sections.initialEquations);
279 ✗ alg := list(algFn(a, arg) for a in sections.algorithms);
280 ✗ ialg := list(ialgFn(a, arg) for a in sections.initialAlgorithms);
281 ✗ sections := SECTIONS(eq, ieq, alg, ialg);
282 then
283 ();
284
285 else ();
286 end match;
287 end map1;
288
289 function mapExp
290 input output Sections sections;
291 input MapFn mapFn;
292
293 partial function MapFn
294 input output Expression exp;
295 end MapFn;
296 protected
297 list<Equation> eq, ieq;
298 list<Algorithm> alg, ialg;
299 algorithm
300 sections := match sections
301 case SECTIONS()
302 algorithm
303 20082 eq := Equation.mapExpList(sections.equations, mapFn);
304 20082 ieq := Equation.mapExpList(sections.initialEquations, mapFn);
305 20082 alg := Algorithm.mapExpList(sections.algorithms, mapFn);
306 20082 ialg := Algorithm.mapExpList(sections.initialAlgorithms, mapFn);
307 20082 then
308 SECTIONS(eq, ieq, alg, ialg);
309
310 case EXTERNAL()
311 algorithm
312
6/6
✓ Branch 0 taken 2192 times.
✓ Branch 1 taken 610 times.
✓ Branch 2 taken 2192 times.
✓ Branch 3 taken 610 times.
✓ Branch 4 taken 1227 times.
✓ Branch 5 taken 965 times.
3412 sections.args := list(mapFn(e) for e in sections.args);
313 then
314 sections;
315
316 else sections;
317 end match;
318 end mapExp;
319
320 function foldExp<ArgT>
321 input Sections sections;
322 input FoldFn foldFn;
323 input output ArgT arg;
324
325 partial function FoldFn
326 input Expression exp;
327 input output ArgT arg;
328 end FoldFn;
329 algorithm
330 arg := match sections
331 case SECTIONS()
332 algorithm
333 25240 arg := Equation.foldExpList(sections.equations, foldFn, arg);
334 25240 arg := Equation.foldExpList(sections.initialEquations, foldFn, arg);
335 25240 arg := Algorithm.foldExpList(sections.algorithms, foldFn, arg);
336 25240 arg := Algorithm.foldExpList(sections.initialAlgorithms, foldFn, arg);
337 then
338 arg;
339
340 case EXTERNAL()
341 5694 then List.fold(sections.args, foldFn, arg);
342
343 else arg;
344 end match;
345 end foldExp;
346
347 function apply
348 input Sections sections;
349 input EquationFn eqFn;
350 input AlgorithmFn algFn;
351 input EquationFn ieqFn = eqFn;
352 input AlgorithmFn ialgFn = algFn;
353
354 partial function EquationFn
355 input Equation eq;
356 end EquationFn;
357
358 partial function AlgorithmFn
359 input Algorithm alg;
360 end AlgorithmFn;
361 algorithm
362 () := match sections
363 case SECTIONS()
364 algorithm
365
2/2
✓ Branch 0 taken 57817 times.
✓ Branch 1 taken 9402 times.
67219 for eq in sections.equations loop
366
1/2
✓ Branch 0 taken 57817 times.
✗ Branch 1 not taken.
57817 eqFn(eq);
367 end for;
368
369
2/2
✓ Branch 0 taken 1649 times.
✓ Branch 1 taken 9402 times.
11051 for ieq in sections.initialEquations loop
370
1/2
✓ Branch 0 taken 1649 times.
✗ Branch 1 not taken.
1649 ieqFn(ieq);
371 end for;
372
373
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 9402 times.
9551 for alg in sections.algorithms loop
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 149 times.
149 algFn(alg);
375 end for;
376
377
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 9402 times.
9454 for ialg in sections.initialAlgorithms loop
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 ialgFn(ialg);
379 end for;
380 then
381 ();
382
383 else ();
384 end match;
385 end apply;
386
387 function isEmpty
388 input Sections sections;
389 output Boolean isEmpty;
390 algorithm
391 isEmpty := match sections
392 case EMPTY() then true;
393 else false;
394 end match;
395 end isEmpty;
396
397 function toStream
398 input Sections sections;
399 input String indent;
400 input output IOStream.IOStream s;
401 algorithm
402 () := match sections
403 case SECTIONS()
404 algorithm
405 ✗ for alg in sections.algorithms loop
406 ✗ s := IOStream.append(s, indent);
407 ✗ s := IOStream.append(s, "algorithm\n");
408 ✗ s := Statement.toStreamList(alg.statements, indent + " ", s);
409 end for;
410 then
411 ();
412
413 case EXTERNAL()
414 algorithm
415 ✗ s := IOStream.append(s, indent);
416 ✗ s := IOStream.append(s, "external \"");
417 ✗ s := IOStream.append(s, sections.language);
418 ✗ s := IOStream.append(s, "\"");
419
420 ✗ if sections.explicit then
421 ✗ if not ComponentRef.isEmpty(sections.outputRef) then
422 ✗ s := IOStream.append(s, " ");
423 ✗ s := IOStream.append(s, ComponentRef.toString(sections.outputRef));
424 ✗ s := IOStream.append(s, " =");
425 end if;
426 ✗ s := IOStream.append(s, " ");
427 ✗ s := IOStream.append(s, sections.name);
428 ✗ s := IOStream.append(s, "(");
429 ✗ s := IOStream.append(s, stringDelimitList(list(Expression.toString(e) for e in sections.args), ", "));
430 ✗ s := IOStream.append(s, ")");
431 end if;
432
433 ✗ if isSome(sections.ann) then
434 ✗ s := IOStream.append(s, DAEDumpTypes.dumpCompAnnotationStr(SOME(SCode.Comment.COMMENT(sections.ann, NONE()))));
435 end if;
436
437 ✗ s := IOStream.append(s, ";\n");
438 then
439 ();
440
441 else ();
442 end match;
443 end toStream;
444
445 function toFlatStream
446 input Sections sections;
447 input Absyn.Path scopeName;
448 input BaseModelica.OutputFormat format;
449 input String indent;
450 input output IOStream.IOStream s;
451 protected
452 SCode.Annotation ann;
453 SCode.Mod mod, modLib, modInc, modLibDir, modIncDir;
454 algorithm
455 () := match sections
456 case SECTIONS()
457 algorithm
458
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 for alg in sections.algorithms loop
459 6 s := IOStream.append(s, indent);
460 6 s := IOStream.append(s, "algorithm\n");
461 6 s := Statement.toFlatStreamList(alg.statements, format, indent + " ", s);
462 end for;
463 then ();
464 case EXTERNAL()
465 algorithm
466 1 s := IOStream.append(s, indent);
467 1 s := IOStream.append(s, "external \"");
468 1 s := IOStream.append(s, sections.language);
469 1 s := IOStream.append(s, "\"");
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if sections.explicit then
471 ✗ if not ComponentRef.isEmpty(sections.outputRef) then
472 ✗ s := IOStream.append(s, " ");
473 ✗ s := IOStream.append(s, ComponentRef.toFlatString(sections.outputRef, format));
474 ✗ s := IOStream.append(s, " =");
475 end if;
476 ✗ s := IOStream.append(s, " ");
477 ✗ s := IOStream.append(s, sections.name);
478 ✗ s := IOStream.append(s, "(");
479 ✗ s := IOStream.append(s, stringDelimitList(list(Expression.toFlatString(e, format) for e in sections.args), ", "));
480 ✗ s := IOStream.append(s, ")");
481 end if;
482
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 if isSome(sections.ann) then
483 ✗ SOME(ann) := sections.ann;
484 ✗ mod := ann.modification;
485 ✗ modLib := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"Library"}));
486 ✗ modInc := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"Include"}));
487 ✗ if SCodeUtil.isEmptyMod(modLib) then
488 modLibDir := SCode.NOMOD();
489 else
490 ✗ modLibDir := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"LibraryDirectory"}));
491 ✗ if SCodeUtil.isEmptyMod(modLibDir) then
492 ✗ modLibDir := SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {SCode.NAMEMOD("LibraryDirectory", SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {}, SOME(Absyn.STRING("modelica://" + AbsynUtil.pathFirstIdent(scopeName) + "/Resources/Library")), NONE(), Error.dummyInfo))}, NONE(), NONE(), Error.dummyInfo);
493 end if;
494 end if;
495 ✗ if SCodeUtil.isEmptyMod(modInc) then
496 modIncDir := SCode.NOMOD();
497 else
498 ✗ modIncDir := SCodeUtil.filterSubMods(mod, function SCodeUtil.filterGivenSubModNames(namesToKeep={"IncludeDirectory"}));
499 ✗ if SCodeUtil.isEmptyMod(modLibDir) then
500 ✗ modLibDir := SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {SCode.NAMEMOD("IncludeDirectory", SCode.MOD(SCode.NOT_FINAL(), SCode.NOT_EACH(), {}, SOME(Absyn.STRING("modelica://" + AbsynUtil.pathFirstIdent(scopeName) + "/Resources/Include")), NONE(), Error.dummyInfo))}, NONE(), NONE(), Error.dummyInfo);
501 end if;
502 end if;
503 ✗ ann.modification := SCodeUtil.mergeSCodeMods(SCodeUtil.mergeSCodeMods(modLib, modLibDir), SCodeUtil.mergeSCodeMods(modInc, modIncDir));
504 ✗ s := IOStream.append(s, SCodeDump.printAnnotationStr(SCode.COMMENT(SOME(ann), NONE())));
505 end if;
506 1 s := IOStream.append(s, ";\n");
507 then ();
508 else ();
509 end match;
510 end toFlatStream;
511
512 annotation(__OpenModelica_Interface="nf_frontend");
513 end NFSections;
514