Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 33.9% 59 / 0 / 174
Functions: -% 0 / 1 / 1
Branches: 8.3% 1 / 0 / 12

OMCompiler/Compiler/FrontEnd/ValuesDump.mo
Line Branch Exec Source
1 /*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC),
5 * c/o Linköpings universitet, Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR
11 * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8.
12 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13 * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL
14 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the OSMC (Open Source Modelica Consortium)
17 * Public License (OSMC-PL) are obtained from OSMC, either from the above
18 * address, from the URLs:
19 * http://www.openmodelica.org or
20 * https://github.com/OpenModelica/ or
21 * http://www.ida.liu.se/projects/OpenModelica,
22 * and in the OpenModelica distribution.
23 *
24 * GNU AGPL version 3 is obtained from:
25 * https://www.gnu.org/licenses/licenses.html#GPL
26 *
27 * This program is distributed WITHOUT ANY WARRANTY; without
28 * even the implied warranty of MERCHANTABILITY or FITNESS
29 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
30 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
31 *
32 * See the full OSMC Public License conditions for more details.
33 *
34 */
35
36 encapsulated package ValuesDump
37 " file: ValuesDump.mo
38 package: ValuesDump
39 description: Evaluated expression values
40
41
42 The package Values contains utility functions for handling evaluated
43 expression values."
44
45 import Absyn;
46 import Values;
47 protected
48 import AbsynUtil;
49 import Dump;
50 import Error;
51 import Flags;
52 import List;
53 import Print;
54 import System;
55
56 public function valString "This function returns a textual representation of a value."
57 input Values.Value inValue;
58 output String outString;
59 protected
60 Integer handle;
61 algorithm
62
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 46361 times.
46361 if Flags.getConfigEnum(Flags.INTERACTIVE_DUMP_FORMAT) == Flags.IDUMP_JSON then
63 ✗ outString := valStringJSON(inValue);
64 ✗ return;
65 end if;
66 46361 handle := Print.saveAndClearBuf();
67 46361 valString2(inValue);
68 46361 outString := Print.getString();
69 46361 Print.restoreBuf(handle);
70 end valString;
71
72 public function valStringJSON "A JSON representation of a value, for programmatic consumers."
73 input Values.Value inValue;
74 output String outString;
75 protected
76 Integer handle;
77 algorithm
78 ✗ handle := Print.saveAndClearBuf();
79 ✗ valJSON(inValue);
80 ✗ outString := Print.getString();
81 ✗ Print.restoreBuf(handle);
82 end valStringJSON;
83
84 protected function valJSON
85 input Values.Value inValue;
86 algorithm
87 () := match inValue
88 local
89 String s;
90 Integer n;
91 Real x;
92 list<Values.Value> xs;
93 list<String> ids;
94 Values.Value v;
95 Absyn.Path p;
96 ✗ case Values.INTEGER(integer = n) algorithm Print.printBuf(intString(n)); then ();
97 ✗ case Values.REAL(real = x) algorithm Print.printBuf(realString(x)); then ();
98 ✗ case Values.BOOL(boolean = true) algorithm Print.printBuf("true"); then ();
99 ✗ case Values.BOOL(boolean = false) algorithm Print.printBuf("false"); then ();
100 ✗ case Values.STRING(string = s) algorithm printJSONString(s); then ();
101 ✗ case Values.ENUM_LITERAL(name = p) algorithm printJSONString(AbsynUtil.pathString(p)); then ();
102 ✗ case Values.ARRAY(valueLst = xs) algorithm valJSONArray(xs); then ();
103 ✗ case Values.LIST(valueLst = xs) algorithm valJSONArray(xs); then ();
104 ✗ case Values.META_ARRAY(valueLst = xs) algorithm valJSONArray(xs); then ();
105 ✗ case Values.TUPLE(valueLst = xs) algorithm valJSONArray(xs); then ();
106 ✗ case Values.META_TUPLE(valueLst = xs) algorithm valJSONArray(xs); then ();
107 ✗ case Values.OPTION(some = SOME(v)) algorithm valJSON(v); then ();
108 ✗ case Values.OPTION(some = NONE()) algorithm Print.printBuf("null"); then ();
109 ✗ case Values.RECORD(orderd = xs, comp = ids) algorithm valJSONRecord(xs, ids); then ();
110 ✗ else algorithm Print.printBuf("null"); then ();
111 end match;
112 end valJSON;
113
114 protected function valJSONArray
115 input list<Values.Value> values;
116 protected
117 Boolean first = true;
118 algorithm
119 ✗ Print.printBuf("[");
120 ✗ for v in values loop
121 ✗ if not first then Print.printBuf(","); end if;
122 first := false;
123 ✗ valJSON(v);
124 end for;
125 ✗ Print.printBuf("]");
126 end valJSONArray;
127
128 protected function valJSONRecord
129 input list<Values.Value> values;
130 input list<String> names;
131 protected
132 Boolean first = true;
133 list<String> rest = names;
134 String nm;
135 algorithm
136 ✗ Print.printBuf("{");
137 ✗ for v in values loop
138 ✗ nm :: rest := rest;
139 ✗ if not first then Print.printBuf(","); end if;
140 first := false;
141 ✗ printJSONString(nm);
142 ✗ Print.printBuf(":");
143 ✗ valJSON(v);
144 end for;
145 ✗ Print.printBuf("}");
146 end valJSONRecord;
147
148 protected function printJSONString
149 "Print a JSON string literal (quoted, escaped) straight to the buffer. escapedString
150 (as JSON.toString uses it) escapes the quote, backslash and \\r\\n, but leaves tab
151 literal — invalid in a JSON string — so escape that too."
152 input String s;
153 algorithm
154 ✗ Print.printBuf("\"");
155 ✗ Print.printBuf(System.stringReplace(System.escapedString(s, true), "\t", "\\t"));
156 ✗ Print.printBuf("\"");
157 end printJSONString;
158
159 public function valString2 "This function returns a textual representation of a value.
160 Uses an external buffer to store intermediate results."
161 input Values.Value inValue;
162 algorithm
163 () := matchcontinue inValue
164 local
165 String s, recordName, tyStr, scope, name;
166 Integer n;
167 Real x;
168 list<Values.Value> xs,vs;
169 Values.Value r;
170 Absyn.CodeNode c;
171 Absyn.Path p, recordPath;
172 list<String> ids;
173 Absyn.ComponentRef cr;
174 Absyn.Path path;
175
176 case Values.INTEGER(integer = n)
177 algorithm
178 3460 s := intString(n);
179 3460 Print.printBuf(s);
180 then
181 ();
182 case Values.REAL(real = x)
183 algorithm
184 22680 s := realString(x);
185 22680 Print.printBuf(s);
186 then
187 ();
188 case Values.STRING(string = s)
189 algorithm
190 35996 Print.printBuf("\"");
191 35996 Print.printBuf(System.escapedString(s, false));
192 35996 Print.printBuf("\"");
193 then
194 ();
195 case Values.BOOL(boolean = false)
196 algorithm
197 961 Print.printBuf("false");
198 then
199 ();
200 case Values.BOOL(boolean = true)
201 algorithm
202 3857 Print.printBuf("true");
203 then
204 ();
205 case Values.ENUM_LITERAL(name = p)
206 algorithm
207 567 s := AbsynUtil.pathString(p);
208 567 Print.printBuf(s);
209 then
210 ();
211 case Values.ARRAY(valueLst = vs)
212 algorithm
213 1671 Print.printBuf("{");
214 1671 valListString(vs);
215 1671 Print.printBuf("}");
216 then
217 ();
218 case Values.TUPLE(valueLst = {})
219 then ();
220 case Values.TUPLE(valueLst = vs)
221 algorithm
222 130 Print.printBuf("(");
223 130 valListString(vs);
224 130 Print.printBuf(")");
225 then
226 ();
227 case Values.META_TUPLE(valueLst = {})
228 then ();
229 case Values.META_TUPLE(valueLst = vs)
230 algorithm
231 127 Print.printBuf("(");
232 127 valListString(vs);
233 127 Print.printBuf(")");
234 then
235 ();
236
237 case Values.RECORD(record_ = Absyn.IDENT("SimulationResult"), orderd = xs, comp = ids)
238 algorithm
239 1172 Print.printBuf("record SimulationResult\n");
240 1172 (xs,ids) := filterSimulationResults(Flags.isSet(Flags.SHORT_OUTPUT),xs,ids,{},{});
241 1172 valRecordString(xs,ids);
242 1172 Print.printBuf("end SimulationResult;");
243 then
244 ();
245
246 case Values.RECORD(record_ = recordPath, orderd = xs, comp = ids)
247 algorithm
248 189 recordName := AbsynUtil.pathStringNoQual(recordPath);
249
250 189 Print.printBuf("record " + recordName + "\n");
251 189 valRecordString(xs,ids);
252 189 Print.printBuf("end " + recordName + ";");
253 then
254 ();
255
256 case Values.OPTION(SOME(r))
257 algorithm
258 11 Print.printBuf("SOME(");
259 11 valString2(r);
260 11 Print.printBuf(")");
261 then
262 ();
263 case Values.OPTION(NONE())
264 algorithm
265 18 Print.printBuf("NONE()");
266 then
267 ();
268
269 case Values.META_BOX(r)
270 algorithm
271 ✗ Print.printBuf("#(");
272 ✗ valString2(r);
273 ✗ Print.printBuf(")");
274 then
275 ();
276
277 case Values.CODE(A = Absyn.C_TYPENAME(path))
278 algorithm
279 739 Print.printBuf(AbsynUtil.pathString(path));
280 then
281 ();
282
283 case Values.CODE(A = Absyn.C_VARIABLENAME(cr))
284 algorithm
285 3 Print.printBuf(Dump.printComponentRefStr(cr));
286 then
287 ();
288
289 case Values.CODE(A = c)
290 algorithm
291 ✗ Print.printBuf("$Code(");
292 ✗ Print.printBuf(Dump.printCodeStr(c));
293 ✗ Print.printBuf(")");
294 then
295 ();
296
297 // MetaModelica list
298 case Values.LIST(valueLst = vs)
299 algorithm
300 209 Print.printBuf("{");
301 209 valListString(vs);
302 209 Print.printBuf("}");
303 then
304 ();
305
306 // MetaModelica array
307 case Values.META_ARRAY(valueLst = vs)
308 algorithm
309 ✗ Print.printBuf("meta_array(");
310 ✗ valListString(vs);
311 ✗ Print.printBuf(")");
312 then
313 ();
314
315 /* Until is it no able to get from an string Enumeration the C-Enumeration use the index value */
316 /* Example: This is yet not possible Enum.e1 \\ PEnum -> 1 \\ PEnum with enum Enum(e1,e2), Enum PEnum; */
317 case Values.ENUM_LITERAL(index = n, name=p)
318 algorithm
319 ✗ s := intString(n) + " /* ENUM: " + AbsynUtil.pathString(p) + " */";
320 ✗ Print.printBuf(s);
321 then
322 ();
323
324 case Values.NORETCALL()
325 then ();
326
327 case Values.META_FAIL()
328 algorithm
329 6 Print.printBuf("fail()");
330 then ();
331
332 case Values.EMPTY(scope = scope, name = name, tyStr = tyStr)
333 algorithm
334 ✗ Print.printBuf("/* <EMPTY(scope: " + scope + ", name: " + name + ", ty: " + tyStr + ")> */");
335 then ();
336
337 else
338 algorithm
339 ✗ Error.addMessage(Error.INTERNAL_ERROR, {"ValuesDump.valString2 failed"});
340 ✗ then
341 fail();
342 end matchcontinue;
343 end valString2;
344
345 protected function filterSimulationResults
346 input Boolean filter;
347 input list<Values.Value> inValues;
348 input list<String> inIds;
349 input list<Values.Value> valacc;
350 input list<String> idacc;
351 output list<Values.Value> outValues;
352 output list<String> outIds;
353 algorithm
354 (outValues,outIds) := match (filter, inValues, inIds)
355 local
356 Values.Value v;
357 list<Values.Value> vrest;
358 String id,str;
359 list<String> idrest;
360 ✗ case (_, {}, {}) then (listReverse(valacc),listReverse(idacc));
361 case (true, v::vrest, (id as "messages")::idrest)
362 algorithm
363 ✗ (outValues,outIds) := filterSimulationResults(filter,vrest,idrest,v::valacc,id::idacc);
364 then (outValues,outIds);
365 case (true, Values.STRING(str)::vrest, (id as "resultFile")::idrest)
366 algorithm
367 ✗ str := System.basename(str);
368 ✗ (outValues,outIds) := filterSimulationResults(filter,vrest,idrest,Values.STRING(str)::valacc,id::idacc);
369 then (outValues,outIds);
370 case (true, _::vrest, _::idrest)
371 algorithm
372 ✗ (outValues,outIds) := filterSimulationResults(filter,vrest,idrest,valacc,idacc);
373 then (outValues,outIds);
374 case (false, _, _) then (inValues,inIds);
375 end match;
376 end filterSimulationResults;
377
378 protected function valRecordString
379 "This function returns a textual representation of a record,
380 separating each value with a comma."
381 input list<Values.Value> inValues;
382 input list<String> inIds;
383 algorithm
384 () := matchcontinue (inValues,inIds)
385 local
386 String id;
387 Values.Value x;
388 list<Values.Value> xs;
389 list<String> ids;
390
391 case ({},{}) then ();
392
393 case (x :: (xs as (_ :: _)),id :: (ids as (_ :: _)))
394 algorithm
395 2599 Print.printBuf(" ");
396 2599 Print.printBuf(id);
397 2599 Print.printBuf(" = ");
398 2599 valString2(x);
399 2599 Print.printBuf(",\n");
400 2599 valRecordString(xs,ids);
401 then
402 ();
403
404 case (x :: {},id :: {})
405 algorithm
406 1357 Print.printBuf(" ");
407 1357 Print.printBuf(id);
408 1357 Print.printBuf(" = ");
409 1357 valString2(x);
410 1357 Print.printBuf("\n");
411 then
412 ();
413
414 case (xs,ids)
415 algorithm
416 ✗ print("ValuesUtil.valRecordString failed:\nids: "+ stringDelimitList(ids, ", ") +
417 "\nvals: " + stringDelimitList(List.map(xs, valString), ", ") + "\n");
418 ✗ then
419 fail();
420
421 end matchcontinue;
422 end valRecordString;
423
424 protected function valListString "
425 This function returns a textual representation of a list of
426 values, separating each value with a comma.
427 "
428 input list<Values.Value> inValueLst;
429 algorithm
430 () := match inValueLst
431 local
432 Values.Value v;
433 list<Values.Value> vs;
434 case {} then ();
435 case {v}
436 algorithm
437 1689 valString2(v);
438 then
439 ();
440 case v :: vs
441 algorithm
442 20265 valString2(v);
443 20265 Print.printBuf(", ");
444 20265 valListString(vs);
445 then
446 ();
447 end match;
448 end valListString;
449
450 public function printVal "This function prints a value."
451 input Values.Value v;
452 protected
453 String s;
454 algorithm
455 ✗ s := valString(v);
456 ✗ Print.printBuf(s);
457 end printVal;
458
459 public function printValStr "
460 more correct naming then valString"
461 input Values.Value v;
462 output String s;
463 algorithm
464 111 s := valString(v);
465 end printValStr;
466
467
468 public function unparseValues "Prints a list of Value to a string."
469 input list<Values.Value> inValueLst;
470 output String outString;
471 algorithm
472 outString := match inValueLst
473 local
474 String s1,s2,s3,str;
475 Values.Value v;
476 list<Values.Value> vallst;
477 case v :: vallst
478 algorithm
479 ✗ s1 := unparseDescription({v});
480 ✗ s2 := unparseValueNumbers({v});
481 ✗ s3 := unparseValues(vallst);
482 ✗ str := stringAppendList({s1,s2,"\n",s3});
483 then
484 str;
485 case {} then "";
486 end match;
487 end unparseValues;
488
489 protected function unparseValueNumbers "Helper function to unparse_values.
490 Prints all the numbers of the values."
491 input list<Values.Value> inValueLst;
492 output String outString;
493 algorithm
494 outString := match inValueLst
495 local
496 String s1,s2,res,istr,sval;
497 list<Values.Value> lst,xs;
498 Integer i;
499 Real r;
500 case Values.TUPLE(valueLst = lst) :: xs
501 algorithm
502 ✗ s1 := unparseValueNumbers(lst);
503 ✗ s2 := unparseValueNumbers(xs);
504 ✗ res := stringAppend(s1, s2);
505 then
506 res;
507 case Values.META_TUPLE(valueLst = lst) :: xs
508 algorithm
509 ✗ s1 := unparseValueNumbers(lst);
510 ✗ s2 := unparseValueNumbers(xs);
511 ✗ res := stringAppend(s1, s2);
512 then
513 res;
514 case Values.ARRAY(valueLst = lst) :: xs
515 algorithm
516 ✗ s1 := unparseValueNumbers(lst);
517 ✗ s2 := unparseValueNumbers(xs);
518 ✗ res := stringAppend(s1, s2);
519 then
520 res;
521 case Values.INTEGER(integer = i) :: xs
522 algorithm
523 ✗ s1 := unparseValueNumbers(xs);
524 ✗ istr := intString(i);
525 ✗ s2 := stringAppend(istr, " ");
526 ✗ res := stringAppend(s2, s1);
527 then
528 res;
529 case Values.REAL(real = r) :: xs
530 algorithm
531 ✗ s1 := unparseValueNumbers(xs);
532 ✗ istr := realString(r);
533 ✗ s2 := stringAppend(istr, " ");
534 ✗ res := stringAppend(s2, s1);
535 then
536 res;
537 case Values.STRING(string = sval) :: xs
538 algorithm
539 ✗ s1 := unparseValueNumbers(xs);
540 ✗ s2 := stringAppend(sval, " ");
541 ✗ res := stringAppend(s2, s1);
542 then
543 res;
544 case {} then "";
545 end match;
546 end unparseValueNumbers;
547
548
549 protected function unparseDescription "
550 Helper function to unparse_values. Creates a description string
551 for the type of the value.
552 "
553 input list<Values.Value> inValueLst;
554 output String outString;
555 algorithm
556 outString:=
557 match inValueLst
558 local
559 String s1,str,slenstr,sval,s2,s4;
560 list<Values.Value> xs,vallst;
561 Integer slen;
562 case Values.INTEGER() :: xs
563 algorithm
564 ✗ s1 := unparseDescription(xs);
565 ✗ str := stringAppend("# i!\n", s1);
566 then
567 str;
568 case Values.REAL() :: xs
569 algorithm
570 ✗ s1 := unparseDescription(xs);
571 ✗ str := stringAppend("# r!\n", s1);
572 then
573 str;
574 case Values.STRING(string = sval) :: xs
575 algorithm
576 ✗ s1 := unparseDescription(xs);
577 ✗ slen := stringLength(sval);
578 ✗ slenstr := intString(slen);
579 ✗ str := stringAppendList({"# s! 1 ",slenstr,"\n",s1});
580 then
581 str;
582 case Values.ARRAY(valueLst = vallst) :: xs
583 algorithm
584 ✗ s1 := unparseDescription(xs);
585 ✗ s2 := unparseArrayDescription(vallst);
586 ✗ s4 := stringAppend(s2, s1);
587 ✗ str := stringAppend(s4, " \n");
588 then
589 str;
590 case {} then "";
591 end match;
592 end unparseDescription;
593
594 protected function unparseArrayDescription "
595 Helper function to unparse_description.
596 "
597 input list<Values.Value> lst;
598 output String str;
599 protected
600 String pt,s1,s2,s3,s4,s5,s6;
601 Integer i1;
602 algorithm
603 ✗ pt := unparsePrimType(lst);
604 ✗ s1 := stringAppend("# ", pt);
605 ✗ s2 := stringAppend(s1, "[");
606 ✗ i1 := unparseNumDims(lst,0);
607 ✗ s3 := intString(i1);
608 ✗ s4 := stringAppend(s2, s3);
609 ✗ s5 := stringAppend(s4, " ");
610 ✗ s6 := unparseDimSizes(lst);
611 ✗ str := stringAppend(s5, s6);
612 end unparseArrayDescription;
613
614 protected function unparsePrimType "
615 Helper function to unparse_array_description.
616 "
617 input list<Values.Value> inValueLst;
618 output String outString;
619 algorithm
620 outString:=
621 match inValueLst
622 local
623 String res;
624 list<Values.Value> elts;
625 case Values.ARRAY(valueLst = elts) :: _
626 algorithm
627 ✗ res := unparsePrimType(elts);
628 then
629 res;
630 case Values.INTEGER() :: _ then "i";
631 case Values.REAL() :: _ then "r";
632 case Values.STRING() :: _ then "s";
633 case Values.BOOL() :: _ then "b";
634 case {} then "{}";
635 else "error";
636 end match;
637 end unparsePrimType;
638
639 protected function unparseNumDims "
640 Helper function to unparse_array_description.
641 "
642 input list<Values.Value> inValueLst;
643 input Integer inInteger;
644 output Integer outInteger;
645 algorithm
646 outInteger:=
647 match inValueLst
648 local
649 list<Values.Value> vals;
650 case Values.ARRAY(valueLst = vals) :: _
651 ✗ then
652 unparseNumDims(vals, inInteger + 1);
653 ✗ else inInteger + 1;
654 end match;
655 end unparseNumDims;
656
657 protected function unparseDimSizes "
658 Helper function to unparse_array_description.
659 "
660 input list<Values.Value> inValueLst;
661 output String outString;
662 algorithm
663 outString:=
664 match inValueLst
665 local
666 Integer i1,len;
667 String s1,s2,s3,res;
668 list<Values.Value> lst,vals;
669 case lst as (Values.ARRAY(valueLst = vals) :: _)
670 algorithm
671 ✗ i1 := listLength(lst);
672 ✗ s1 := intString(i1);
673 ✗ s2 := stringAppend(s1, " ");
674 ✗ s3 := unparseDimSizes(vals);
675 ✗ res := stringAppend(s2, s3);
676 then
677 res;
678 case lst
679 algorithm
680 ✗ len := listLength(lst);
681 ✗ res := intString(len);
682 then
683 res;
684 end match;
685 end unparseDimSizes;
686
687 annotation(__OpenModelica_Interface="frontend_dump");
688 end ValuesDump;
689