/
SCode.mo
3975 lines (3514 loc) · 120 KB
/
SCode.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package SCode
" file: SCode.mo
package: SCode
description: SCode intermediate form
RCS: $Id$
This module contains data structures to describe a Modelica
model in a more convenient (canonical) way than the Absyn module does.
Local functions for query of SCode are defined.
Printing and translating to string functions are now moved to SCodeDump! (2011-05-21)
See also SCodeUtil.mo for translation functions from Absyn representation to SCode representation.
The SCode representation is used as input to the Inst module"
public import Absyn;
// Some definitions are aliased from Absyn
public type Ident = Absyn.Ident;
public type Path = Absyn.Path;
public type Subscript = Absyn.Subscript;
public
uniontype Restriction
record R_CLASS end R_CLASS;
record R_OPTIMIZATION end R_OPTIMIZATION;
record R_MODEL end R_MODEL;
record R_RECORD end R_RECORD;
record R_BLOCK end R_BLOCK;
record R_CONNECTOR "a connector"
Boolean isExpandable "is expandable?";
end R_CONNECTOR;
record R_OPERATOR end R_OPERATOR;
record R_OPERATOR_RECORD end R_OPERATOR_RECORD;
record R_TYPE end R_TYPE;
record R_PACKAGE end R_PACKAGE;
record R_FUNCTION
FunctionRestriction functionRestriction;
end R_FUNCTION;
record R_ENUMERATION end R_ENUMERATION;
// predefined internal types
record R_PREDEFINED_INTEGER "predefined IntegerType" end R_PREDEFINED_INTEGER;
record R_PREDEFINED_REAL "predefined RealType" end R_PREDEFINED_REAL;
record R_PREDEFINED_STRING "predefined StringType" end R_PREDEFINED_STRING;
record R_PREDEFINED_BOOLEAN "predefined BooleanType" end R_PREDEFINED_BOOLEAN;
record R_PREDEFINED_ENUMERATION "predefined EnumType" end R_PREDEFINED_ENUMERATION;
// MetaModelica extensions
record R_METARECORD "Metamodelica extension"
Absyn.Path name; //Name of the uniontype
Integer index; //Index in the uniontype
Boolean singleton;
end R_METARECORD; /* added by x07simbj */
record R_UNIONTYPE "Metamodelica extension"
end R_UNIONTYPE; /* added by simbj */
end Restriction;
// Same as Absyn.FunctionRestriction except this contains
// FR_EXTERNAL_FUNCTION and FR_RECORD_CONSTRUCTOR.
public
uniontype FunctionRestriction
record FR_NORMAL_FUNCTION "a normal function" end FR_NORMAL_FUNCTION;
record FR_OPERATOR_FUNCTION "an operator function" end FR_OPERATOR_FUNCTION;
record FR_EXTERNAL_FUNCTION "an external function" end FR_EXTERNAL_FUNCTION;
record FR_RECORD_CONSTRUCTOR "record constructor" end FR_RECORD_CONSTRUCTOR;
record FR_PARALLEL_FUNCTION "an OpenCL/CUDA parallel/device function" end FR_PARALLEL_FUNCTION;
record FR_KERNEL_FUNCTION "an OpenCL/CUDA kernel function" end FR_KERNEL_FUNCTION;
end FunctionRestriction;
public
uniontype Mod "- Modifications"
record MOD
Final finalPrefix "final prefix";
Each eachPrefix "each prefix";
list<SubMod> subModLst;
Option<tuple<Absyn.Exp,Boolean>> binding "The binding expression of a modification
has an expression and a Boolean delayElaboration which is true if elaboration(type checking)
should be delayed. This can for instance be used when having A a(x = a.y) where a.y can not be
type checked -before- a is instantiated, which is the current design in instantiation process.";
Absyn.Info info;
end MOD;
record REDECL
Final finalPrefix "final prefix";
Each eachPrefix "each prefix";
Element element "The new element declaration.";
end REDECL;
record NOMOD end NOMOD;
end Mod;
public
uniontype SubMod "Modifications are represented in an more structured way than in
the `Absyn\' module. Modifications using qualified names
(such as in `x.y = z\') are normalized (to `x(y = z)\'). And a
special case when arrays are subscripted in a modification.
"
record NAMEMOD
Ident ident;
Mod A "A named component" ;
end NAMEMOD;
record IDXMOD
list<Subscript> subscriptLst;
Mod an "An array element" ;
end IDXMOD;
end SubMod;
public
type Program = list<Element> "- Programs
As in the AST, a program is simply a list of class definitions.";
public
uniontype Enum "Enum, which is a name in an enumeration and an optional Comment."
record ENUM
Ident literal;
Option<Comment> comment;
end ENUM;
end Enum;
public
uniontype ClassDef
"The major difference between these types and their Absyn
counterparts is that the PARTS constructor contains separate
lists for elements, equations and algorithms.
SCode.PARTS contains elements of a class definition. For instance,
model A
extends B;
C c;
end A;
Here PARTS contains two elements ('extends B' and 'C c')
SCode.DERIVED is used for short class definitions, i.e:
class A = B[ArrayDims](modifiers);
SCode.CLASS_EXTENDS is used for extended class definition, i.e:
class extends A (modifier)
new elements;
end A;"
record PARTS "a class made of parts"
list<Element> elementLst "the list of elements";
list<Equation> normalEquationLst "the list of equations";
list<Equation> initialEquationLst "the list of initial equations";
list<AlgorithmSection> normalAlgorithmLst "the list of algorithms";
list<AlgorithmSection> initialAlgorithmLst "the list of initial algorithms";
list<ConstraintSection> constraintLst "the list of constraints";
list<Absyn.NamedArg> clsattrs "the list of class attributes. Currently for Optimica extensions";
Option<ExternalDecl> externalDecl "used by external functions";
list<Annotation> annotationLst "the list of annotations found in between class elements, equations and algorithms";
Option<Comment> comment "the class comment";
end PARTS;
record CLASS_EXTENDS "an extended class definition plus the additional parts"
Ident baseClassName "the name of the base class we have to extend";
Mod modifications "the modifications that need to be applied to the base class";
ClassDef composition "the new composition";
end CLASS_EXTENDS;
record DERIVED "a derived class"
Absyn.TypeSpec typeSpec "typeSpec: type specification" ;
Mod modifications "the modifications";
Attributes attributes "the element attributes";
Option<Comment> comment "the translated comment from the Absyn";
end DERIVED;
record ENUMERATION "an enumeration"
list<Enum> enumLst "if the list is empty it means :, the supertype of all enumerations";
Option<Comment> comment "the translated comment from the Absyn";
end ENUMERATION;
record OVERLOAD "an overloaded function"
list<Absyn.Path> pathLst "the path lists";
Option<Comment> comment "the translated comment from the Absyn";
end OVERLOAD;
record PDER "the partial derivative"
Absyn.Path functionPath "function name" ;
list<Ident> derivedVariables "derived variables" ;
Option<Comment> comment "the Absyn comment";
end PDER;
end ClassDef;
// stefan
public
uniontype Comment
record COMMENT
Option<Annotation> annotation_;
Option<String> comment;
end COMMENT;
record CLASS_COMMENT
list<Annotation> annotations;
Option<Comment> comment;
end CLASS_COMMENT;
end Comment;
// stefan
public
uniontype Annotation
record ANNOTATION
Mod modification;
end ANNOTATION;
end Annotation;
public
uniontype ExternalDecl "Declaration of an external function call - ExternalDecl"
record EXTERNALDECL
Option<Ident> funcName "The name of the external function" ;
Option<String> lang "Language of the external function" ;
Option<Absyn.ComponentRef> output_ "output parameter as return value" ;
list<Absyn.Exp> args "only positional arguments, i.e. expression list" ;
Option<Annotation> annotation_ ;
end EXTERNALDECL;
end ExternalDecl;
public
uniontype Equation "- Equations"
record EQUATION "an equation"
EEquation eEquation "an equation";
end EQUATION;
end Equation;
public
uniontype EEquation
"These represent equations and are almost identical to their Absyn versions.
In EQ_IF the elseif branches are represented as normal else branches with
a single if statement in them."
record EQ_IF
list<Absyn.Exp> condition "conditional" ;
list<list<EEquation>> thenBranch "the true (then) branch" ;
list<EEquation> elseBranch "the false (else) branch" ;
Option<Comment> comment;
Absyn.Info info;
end EQ_IF;
record EQ_EQUALS "the equality equation"
Absyn.Exp expLeft "the expression on the left side of the operator";
Absyn.Exp expRight "the expression on the right side of the operator";
Option<Comment> comment;
Absyn.Info info;
end EQ_EQUALS;
record EQ_CONNECT "the connect equation"
Absyn.ComponentRef crefLeft "the connector/component reference on the left side";
Absyn.ComponentRef crefRight "the connector/component reference on the right side";
Option<Comment> comment;
Absyn.Info info;
end EQ_CONNECT;
record EQ_FOR "the for equation"
Ident index "the index name";
Option<Absyn.Exp> range "the range of the index";
list<EEquation> eEquationLst "the equation list";
Option<Comment> comment;
Absyn.Info info;
end EQ_FOR;
record EQ_WHEN "the when equation"
Absyn.Exp condition "the when condition";
list<EEquation> eEquationLst "the equation list";
list<tuple<Absyn.Exp, list<EEquation>>> elseBranches "the elsewhen expression and equation list";
Option<Comment> comment;
Absyn.Info info;
end EQ_WHEN;
record EQ_ASSERT "the assert equation"
Absyn.Exp condition "the assert condition";
Absyn.Exp message "the assert message";
Absyn.Exp level;
Option<Comment> comment;
Absyn.Info info;
end EQ_ASSERT;
record EQ_TERMINATE "the terminate equation"
Absyn.Exp message "the terminate message";
Option<Comment> comment;
Absyn.Info info;
end EQ_TERMINATE;
record EQ_REINIT "a reinit equation"
Absyn.ComponentRef cref "the variable to initialize";
Absyn.Exp expReinit "the new value" ;
Option<Comment> comment;
Absyn.Info info;
end EQ_REINIT;
record EQ_NORETCALL "function calls without return value"
Absyn.Exp exp;
Option<Comment> comment;
Absyn.Info info;
end EQ_NORETCALL;
end EEquation;
public uniontype AlgorithmSection "- Algorithms
The Absyn module uses the terminology from the
grammar, where algorithm means an algorithmic
statement. But here, an Algorithm means a whole
algorithm section."
record ALGORITHM "the algorithm section"
list<Statement> statements "the algorithm statements" ;
end ALGORITHM;
end AlgorithmSection;
public uniontype ConstraintSection
record CONSTRAINTS
list<Absyn.Exp> constraints;
end CONSTRAINTS;
end ConstraintSection;
public uniontype Statement "The Statement type describes one algorithm statement in an algorithm section."
record ALG_ASSIGN
Absyn.Exp assignComponent "assignComponent" ;
Absyn.Exp value "value" ;
Option<Comment> comment;
Absyn.Info info;
end ALG_ASSIGN;
record ALG_IF
Absyn.Exp boolExpr;
list<Statement> trueBranch;
list<tuple<Absyn.Exp, list<Statement>>> elseIfBranch;
list<Statement> elseBranch;
Option<Comment> comment;
Absyn.Info info;
end ALG_IF;
record ALG_FOR
Ident index "the index name";
Option<Absyn.Exp> range "the range of the index";
list<Statement> forBody "forBody";
Option<Comment> comment;
Absyn.Info info;
end ALG_FOR;
record ALG_PARFOR
Ident index "the index name";
Option<Absyn.Exp> range "the range of the index";
list<Statement> parforBody "parallel for loop body";
Option<Comment> comment;
Absyn.Info info;
end ALG_PARFOR;
record ALG_WHILE
Absyn.Exp boolExpr "boolExpr" ;
list<Statement> whileBody "whileBody" ;
Option<Comment> comment;
Absyn.Info info;
end ALG_WHILE;
record ALG_WHEN_A
list<tuple<Absyn.Exp, list<Statement>>> branches;
Option<Comment> comment;
Absyn.Info info;
end ALG_WHEN_A;
record ALG_NORETCALL
Absyn.Exp exp;
Option<Comment> comment;
Absyn.Info info;
end ALG_NORETCALL;
record ALG_RETURN
Option<Comment> comment;
Absyn.Info info;
end ALG_RETURN;
record ALG_BREAK
Option<Comment> comment;
Absyn.Info info;
end ALG_BREAK;
// Part of MetaModelica extension. KS
record ALG_TRY
list<Statement> tryBody;
Option<Comment> comment;
Absyn.Info info;
end ALG_TRY;
record ALG_CATCH
list<Statement> catchBody;
Option<Comment> comment;
Absyn.Info info;
end ALG_CATCH;
record ALG_THROW
Option<Comment> comment;
Absyn.Info info;
end ALG_THROW;
record ALG_FAILURE
list<Statement> stmts;
Option<Comment> comment;
Absyn.Info info;
end ALG_FAILURE;
//-------------------------------
end Statement;
// common prefixes to elements
public
uniontype Visibility "the visibility prefix"
record PUBLIC "a public element" end PUBLIC;
record PROTECTED "a protected element" end PROTECTED;
end Visibility;
public
uniontype Redeclare "the redeclare prefix"
record REDECLARE "a redeclare prefix" end REDECLARE;
record NOT_REDECLARE "a non redeclare prefix" end NOT_REDECLARE;
end Redeclare;
public uniontype ConstrainClass
record CONSTRAINCLASS
Absyn.Path constrainingClass;
Mod modifier;
Option<Comment> comment;
end CONSTRAINCLASS;
end ConstrainClass;
public
uniontype Replaceable "the replaceable prefix"
record REPLACEABLE "a replaceable prefix containing an optional constraint"
Option<ConstrainClass> cc "the constraint class";
end REPLACEABLE;
record NOT_REPLACEABLE "a non replaceable prefix" end NOT_REPLACEABLE;
end Replaceable;
public
uniontype Final "the final prefix"
record FINAL "a final prefix" end FINAL;
record NOT_FINAL "a non final prefix" end NOT_FINAL;
end Final;
public
uniontype Each "the each prefix"
record EACH "a each prefix" end EACH;
record NOT_EACH "a non each prefix" end NOT_EACH;
end Each;
public
uniontype Encapsulated "the encapsulated prefix"
record ENCAPSULATED "a encapsulated prefix" end ENCAPSULATED;
record NOT_ENCAPSULATED "a non encapsulated prefix" end NOT_ENCAPSULATED;
end Encapsulated;
public
uniontype Partial "the partial prefix"
record PARTIAL "a partial prefix" end PARTIAL;
record NOT_PARTIAL "a non partial prefix" end NOT_PARTIAL;
end Partial;
public
uniontype ConnectorType
record POTENTIAL "No connector type prefix." end POTENTIAL;
record FLOW "A flow prefix." end FLOW;
record STREAM "A stream prefix." end STREAM;
end ConnectorType;
public
uniontype Prefixes "the common class or component prefixes"
record PREFIXES "the common class or component prefixes"
Visibility visibility "the protected/public prefix";
Redeclare redeclarePrefix "redeclare prefix";
Final finalPrefix "final prefix, be it at the element or top level";
Absyn.InnerOuter innerOuter "the inner/outer/innerouter prefix";
Replaceable replaceablePrefix "replaceable prefix";
end PREFIXES;
end Prefixes;
public
uniontype Element "- Elements
There are four types of elements in a declaration, represented by the constructors:
IMPORT (for import clauses)
EXTENDS (for extends clauses),
CLASS (for top/local class definitions)
COMPONENT (for local variables)
DEFINEUNIT (for units)"
record IMPORT "an import element"
Absyn.Import imp "the import definition";
Visibility visibility "the protected/public prefix";
Absyn.Info info "the import information";
end IMPORT;
record EXTENDS "the extends element"
Path baseClassPath "the extends path";
Visibility visibility "the protected/public prefix";
Mod modifications "the modifications applied to the base class";
Option<Annotation> ann "the extends annotation";
Absyn.Info info "the extends info";
end EXTENDS;
record CLASS "a class definition"
Ident name "the name of the class";
Prefixes prefixes "the common class or component prefixes";
Encapsulated encapsulatedPrefix "the encapsulated prefix";
Partial partialPrefix "the partial prefix";
Restriction restriction "the restriction of the class";
ClassDef classDef "the class specification";
Absyn.Info info "the class information";
end CLASS;
record COMPONENT "a component"
Ident name "the component name";
Prefixes prefixes "the common class or component prefixes";
Attributes attributes "the component attributes";
Absyn.TypeSpec typeSpec "the type specification";
Mod modifications "the modifications to be applied to the component";
Option<Comment> comment "this if for extraction of comments and annotations from Absyn";
Option<Absyn.Exp> condition "the conditional declaration of a component";
Absyn.Info info "this is for line and column numbers, also file name.";
end COMPONENT;
record DEFINEUNIT "a unit defintion has a name and the two optional parameters exp, and weight"
Ident name;
Visibility visibility "the protected/public prefix";
Option<String> exp "the unit expression";
Option<Real> weight "the weight";
end DEFINEUNIT;
end Element;
public
uniontype Attributes "- Attributes"
record ATTR "the attributes of the component"
Absyn.ArrayDim arrayDims "the array dimensions of the component";
ConnectorType connectorType "The connector type: flow, stream or nothing.";
Parallelism parallelism "parallelism prefix: parglobal, parlocal, parprivate";
Variability variability " the variability: parameter, discrete, variable, constant" ;
Absyn.Direction direction "the direction: input, output or bidirectional" ;
end ATTR;
end Attributes;
public
uniontype Parallelism "Parallelism"
record PARGLOBAL "Global variables for CUDA and OpenCL" end PARGLOBAL;
record PARLOCAL "Shared for CUDA and local for OpenCL" end PARLOCAL;
record NON_PARALLEL "Non parallel/Normal variables" end NON_PARALLEL;
end Parallelism;
public
uniontype Variability "the variability of a component"
record VAR "a variable" end VAR;
record DISCRETE "a discrete variable" end DISCRETE;
record PARAM "a parameter" end PARAM;
record CONST "a constant" end CONST;
end Variability;
public /* adrpo: previously present in Inst.mo */
uniontype Initial "the initial attribute of an algorithm or equation
Intial is used as argument to instantiation-function for
specifying if equations or algorithms are initial or not."
record INITIAL "an initial equation or algorithm" end INITIAL;
record NON_INITIAL "a normal equation or algorithm" end NON_INITIAL;
end Initial;
public constant Prefixes defaultPrefixes =
PREFIXES(
PUBLIC(),
NOT_REDECLARE(),
NOT_FINAL(),
Absyn.NOT_INNER_OUTER(),
NOT_REPLACEABLE());
public constant Attributes defaultVarAttr =
ATTR({}, POTENTIAL(), NON_PARALLEL(), VAR(), Absyn.BIDIR());
public constant Attributes defaultConstAttr =
ATTR({}, POTENTIAL(), NON_PARALLEL(), CONST(), Absyn.BIDIR());
// .......... functionality .........
protected import Util;
protected import List;
protected import SCodeCheck;
public function stripSubmod
"Removes all submodifiers from the Mod."
input Mod inMod;
output Mod outMod;
algorithm
outMod := match(inMod)
local
Final fp;
Each ep;
Option<tuple<Absyn.Exp, Boolean>> binding;
Absyn.Info info;
case MOD(fp, ep, _, binding, info)
then MOD(fp, ep, {}, binding, info);
else inMod;
end match;
end stripSubmod;
public function getElementNamed
"function: getElementNamed
Return the Element with the name given as first argument from the Class."
input Ident inIdent;
input Element inClass;
output Element outElement;
algorithm
outElement := match (inIdent,inClass)
local
Element elt;
String id;
list<Element> elts;
case (id,CLASS(classDef = PARTS(elementLst = elts)))
equation
elt = getElementNamedFromElts(id, elts);
then
elt;
/* adrpo: handle also the case model extends X then X; */
case (id,CLASS(classDef = CLASS_EXTENDS(composition = PARTS(elementLst = elts))))
equation
elt = getElementNamedFromElts(id, elts);
then
elt;
end match;
end getElementNamed;
protected function getElementNamedFromElts
"function: getElementNamedFromElts
Helper function to getElementNamed."
input Ident inIdent;
input list<Element> inElementLst;
output Element outElement;
algorithm
outElement := matchcontinue (inIdent,inElementLst)
local
Element elt,comp,cdef;
String id2,id1;
list<Element> xs;
case (id2,((comp as COMPONENT(name = id1)) :: _))
equation
true = stringEq(id1, id2);
then
comp;
case (id2,(COMPONENT(name = id1) :: xs))
equation
false = stringEq(id1, id2);
elt = getElementNamedFromElts(id2, xs);
then
elt;
case (id2,(CLASS(name = id1) :: xs))
equation
false = stringEq(id1, id2);
elt = getElementNamedFromElts(id2, xs);
then
elt;
case (id2,(EXTENDS(baseClassPath = _) :: xs))
equation
elt = getElementNamedFromElts(id2, xs);
then
elt;
case (id2,((cdef as CLASS(name = id1)) :: _))
equation
true = stringEq(id1, id2);
then
cdef;
// Try next.
case (id2, _:: xs)
equation
elt = getElementNamedFromElts(id2, xs);
then
elt;
end matchcontinue;
end getElementNamedFromElts;
public function isElementExtends "
Author BZ, 2009-01
check if an element is of type EXTENDS or not."
input Element ele;
output Boolean isExtend;
algorithm isExtend := matchcontinue(ele)
case(EXTENDS(baseClassPath = _)) then true;
case(_) then false;
end matchcontinue;
end isElementExtends;
public function isNotElementClassExtends "
check if an element is not of type CLASS_EXTENDS."
input Element ele;
output Boolean isExtend;
algorithm
isExtend := matchcontinue(ele)
case(CLASS(classDef = CLASS_EXTENDS(baseClassName = _))) then false;
case(_) then true;
end matchcontinue;
end isNotElementClassExtends;
public function isParameterOrConst
"function: isParameterOrConst
Returns true if Variability indicates a parameter or constant."
input Variability inVariability;
output Boolean outBoolean;
algorithm
outBoolean := match (inVariability)
case PARAM() then true;
case CONST() then true;
else false;
end match;
end isParameterOrConst;
public function isConstant
"function: isConstant
Returns true if Variability is constant, otherwise false"
input Variability inVariability;
output Boolean outBoolean;
algorithm
outBoolean := match (inVariability)
case CONST() then true;
else false;
end match;
end isConstant;
public function countParts
"function: countParts
Counts the number of ClassParts of a Class."
input Element inClass;
output Integer outInteger;
algorithm
outInteger := matchcontinue (inClass)
local
Integer res;
list<Element> elts;
case CLASS(classDef = PARTS(elementLst = elts))
equation
res = listLength(elts);
then
res;
/* adrpo: handle also model extends X ... parts ... end X; */
case CLASS(classDef = CLASS_EXTENDS(composition = PARTS(elementLst = elts)))
equation
res = listLength(elts);
then
res;
case _ then 0;
end matchcontinue;
end countParts;
public function componentNames
"function: componentNames
Return a string list of all component names of a class."
input Element inClass;
output list<String> outStringLst;
algorithm
outStringLst := matchcontinue (inClass)
local list<String> res; list<Element> elts;
case (CLASS(classDef = PARTS(elementLst = elts)))
equation
res = componentNamesFromElts(elts);
then
res;
/* adrpo: handle also the case model extends X end X;*/
case (CLASS(classDef = CLASS_EXTENDS(composition = PARTS(elementLst = elts))))
equation
res = componentNamesFromElts(elts);
then
res;
case (_) then {};
end matchcontinue;
end componentNames;
public function elementInfo "retrieves the element info"
input Element e;
output Absyn.Info info;
algorithm
info := match(e)
local
Absyn.Info i;
case(IMPORT(info = i)) then i;
case(EXTENDS(info = i)) then i;
case(CLASS(info = i)) then i;
case(COMPONENT(info = i)) then i;
case(DEFINEUNIT(name = _)) then fail();
end match;
end elementInfo;
public function elementName ""
input Element e;
output String s;
algorithm
s := match(e)
case(COMPONENT(name = s)) then s;
case(CLASS(name = s)) then s;
end match;
end elementName;
public function renameElement
input Element inElement;
input String inName;
output Element outElement;
algorithm
outElement := match(inElement, inName)
local
Prefixes pf;
Encapsulated ep;
Partial pp;
Restriction res;
ClassDef cdef;
Absyn.Info i;
Attributes attr;
Absyn.TypeSpec ty;
Mod mod;
Option<Comment> cmt;
Option<Absyn.Exp> cond;
case (CLASS(_, pf, ep, pp, res, cdef, i), _)
then CLASS(inName, pf, ep, pp, res, cdef, i);
case (COMPONENT(_, pf, attr, ty, mod, cmt, cond, i), _)
then COMPONENT(inName, pf, attr, ty, mod, cmt, cond, i);
end match;
end renameElement;
public function enumName ""
input Enum e;
output String s;
algorithm
s := match(e)
case(ENUM(literal = s)) then s;
end match;
end enumName;
public function componentNamesFromElts
"function: componentNamesFromElts
Helper function to componentNames."
input list<Element> inElementLst;
output list<String> outStringLst;
algorithm
outStringLst := matchcontinue (inElementLst)
local list<String> res; String id; list<Element> rest;
case ({}) then {};
case ((COMPONENT(name = id) :: rest))
equation
res = componentNamesFromElts(rest);
then
(id :: res);
case _ :: rest
then componentNamesFromElts(rest);
end matchcontinue;
end componentNamesFromElts;
public function isRecord
"function: isRecord
Return true if Class is a record."
input Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (inClass)
case CLASS(restriction = R_RECORD()) then true;
case _ then false;
end matchcontinue;
end isRecord;
public function isFunction
"function: isFunction
Return true if Class is a function."
input Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (inClass)
case CLASS(restriction = R_FUNCTION(_)) then true;
case _ then false;
end matchcontinue;
end isFunction;
public function isFunctionRestriction
"function: isFunctionRestriction
Return true if restriction is a function."
input Restriction inRestriction;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (inRestriction)
case R_FUNCTION(_) then true;
case _ then false;
end matchcontinue;
end isFunctionRestriction;
public function isFunctionOrExtFunctionRestriction
"function isFunctionOrExtFunction
This function returns true if the class
restriction is function or external function.
Otherwise false is returned."
input Restriction r;
output Boolean res;
algorithm
res := matchcontinue(r)
case (R_FUNCTION(FR_NORMAL_FUNCTION())) then true;
case (R_FUNCTION(FR_EXTERNAL_FUNCTION())) then true;
case(_) then false;
end matchcontinue;
end isFunctionOrExtFunctionRestriction;
public function isOperator
"function isOperator
This function returns true if the class
restriction is operator or operator function.
Otherwise false is returned."
input Element el;
output Boolean res;
algorithm
res := matchcontinue(el)
case (CLASS(_,_,_,_,R_OPERATOR(),_,_)) then true;
case (CLASS(_,_,_,_,R_FUNCTION(FR_OPERATOR_FUNCTION()),_,_)) then true;
case(_) then false;
end matchcontinue;
end isOperator;
public function className
"function: className
Returns the class name of a Class."
input Element inClass;
output String outString;
algorithm
outString := matchcontinue (inClass)
local String n;
case CLASS(name = n) then n;
case _ then "Not a class";
end matchcontinue;
end className;
public function classSetPartial
"function: classSetPartial
author: PA
Sets the partial attribute of a Class"
input Element inClass;
input Partial inPartial;
output Element outClass;
algorithm
outClass := match (inClass,inPartial)
local