Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 4c362d1

Browse files
perostOpenModelica-Hudson
authored andcommitted
Improved function handling in NFInst.
- Added new uniontype NFCall.Call, and changed NFExpression.CALL to only contain an instance of NFCall.Call. - Moved NFExpression.CallAttributes to NFCall, and changed NFExpression to be a uniontype instead of package. - Added new package NFBuiltinFunc, for defining builtin functions that can't be declared in ModelicaBuiltin (currently Integer and String). - Implemented typing and type checking of function calls. - Implemented flattening of functions and generation of a DAE.FunctionTree during flattening. - Added lots of error checking for functions and calls. - Added Type.ENUMERATION_ANY() to represent enumeration(:). - Implemented support for overloaded functions using $overload.
1 parent 2a857f1 commit 4c362d1

23 files changed

+1399
-498
lines changed

Compiler/NFFrontEnd/NFBinding.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
encapsulated uniontype NFBinding
3333
public
3434
import DAE;
35-
import NFExpression.Expression;
35+
import Expression = NFExpression;
3636
import NFInstNode.InstNode;
3737
import SCode;
3838
import Type = NFType;

Compiler/NFFrontEnd/NFBuiltin.mo

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ import Binding = NFBinding;
4848
import NFClass.Class;
4949
import NFClass.ClassTree;
5050
import NFComponent.Component;
51-
import NFExpression.Expression;
51+
import Expression = NFExpression;
5252
import NFInstNode.InstNode;
5353
import NFInstNode.InstNodeType;
5454
import NFMod.Modifier;
5555
import Type = NFType;
56+
import BuiltinFuncs = NFBuiltinFuncs;
5657

5758
encapsulated package Elements
5859
import SCode;
@@ -86,23 +87,27 @@ encapsulated package Elements
8687
constant SCode.Element REAL = SCode.CLASS("Real",
8788
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
8889
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
89-
SCode.noComment,Absyn.dummyInfo);
90+
SCode.noComment, Absyn.dummyInfo);
9091

9192
constant SCode.Element INTEGER = SCode.CLASS("Integer",
9293
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
9394
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
94-
SCode.noComment,Absyn.dummyInfo);
95+
SCode.noComment, Absyn.dummyInfo);
9596

9697
constant SCode.Element BOOLEAN = SCode.CLASS("Boolean",
9798
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
9899
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
99-
SCode.noComment,Absyn.dummyInfo);
100+
SCode.noComment, Absyn.dummyInfo);
100101

101102
constant SCode.Element STRING = SCode.CLASS("String",
102103
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
103104
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
104-
SCode.noComment,Absyn.dummyInfo);
105+
SCode.noComment, Absyn.dummyInfo);
105106

107+
constant SCode.Element ENUMERATION = SCode.CLASS("enumeration",
108+
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
109+
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
110+
SCode.noComment, Absyn.dummyInfo);
106111

107112
constant SCode.Element STATESELECT = SCode.CLASS("StateSelect",
108113
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
@@ -134,7 +139,7 @@ constant InstNode REAL_TYPE = InstNode.CLASS_NODE("Real",
134139
constant InstNode INT_TYPE = InstNode.CLASS_NODE("Integer",
135140
Elements.INTEGER,
136141
listArray({Class.PARTIAL_BUILTIN(Type.INTEGER(), ClassTree.EMPTY(), listArray({}), Modifier.NOMOD())}),
137-
listArray({NFInstNode.CachedData.NO_CACHE()}),
142+
listArray({NFInstNode.CachedData.FUNCTION({NFBuiltinFuncs.INTEGER}, true)}),
138143
InstNode.EMPTY_NODE(), InstNodeType.NORMAL_CLASS());
139144

140145
constant InstNode BOOLEAN_TYPE = InstNode.CLASS_NODE("Boolean",
@@ -146,6 +151,15 @@ constant InstNode BOOLEAN_TYPE = InstNode.CLASS_NODE("Boolean",
146151
constant InstNode STRING_TYPE = InstNode.CLASS_NODE("String",
147152
Elements.STRING,
148153
listArray({Class.PARTIAL_BUILTIN(Type.STRING(), ClassTree.EMPTY(), listArray({}), Modifier.NOMOD())}),
154+
listArray({NFInstNode.CachedData.FUNCTION({
155+
NFBuiltinFuncs.STRING_ENUM, NFBuiltinFuncs.STRING_INT,
156+
NFBuiltinFuncs.STRING_BOOL, NFBuiltinFuncs.STRING_REAL,
157+
NFBuiltinFuncs.STRING_REAL_FORMAT}, true)}),
158+
InstNode.EMPTY_NODE(), InstNodeType.NORMAL_CLASS());
159+
160+
constant InstNode ENUM_TYPE = InstNode.CLASS_NODE("enumeration",
161+
Elements.ENUMERATION,
162+
listArray({Class.PARTIAL_BUILTIN(Type.ENUMERATION_ANY(), ClassTree.EMPTY(), listArray({}), Modifier.NOMOD())}),
149163
listArray({NFInstNode.CachedData.NO_CACHE()}),
150164
InstNode.EMPTY_NODE(), InstNodeType.NORMAL_CLASS());
151165

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-2014, 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 GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the Open Source Modelica
17+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
18+
* from OSMC, either from the above address,
19+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20+
* http://www.openmodelica.org, and in the OpenModelica distribution.
21+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22+
*
23+
* This program is distributed WITHOUT ANY WARRANTY; without
24+
* even the implied warranty of MERCHANTABILITY or FITNESS
25+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27+
*
28+
* See the full OSMC Public License conditions for more details.
29+
*
30+
*/
31+
32+
encapsulated package NFBuiltinFuncs
33+
34+
import NFFunction.Function;
35+
import NFFunction.Slot;
36+
import NFFunction.SlotType;
37+
import NFFunction.FuncType;
38+
import NFInstNode.InstNode;
39+
import NFInstNode.InstNodeType;
40+
import NFComponent.Component;
41+
import Type = NFType;
42+
import Expression = NFExpression;
43+
import Absyn;
44+
import Absyn.{Path, TypeSpec};
45+
import SCode;
46+
import SCode.{Mod, Comment};
47+
import DAE;
48+
import Builtin = NFBuiltin;
49+
import Binding = NFBinding;
50+
51+
// Dummy SCode component, since we usually don't need the definition for anything.
52+
constant SCode.Element DUMMY_ELEMENT = SCode.COMPONENT("dummy",
53+
SCode.defaultPrefixes, SCode.defaultVarAttr,
54+
TypeSpec.TPATH(Path.IDENT("$dummy"), NONE()), SCode.Mod.NOMOD(),
55+
SCode.Comment.COMMENT(NONE(), NONE()), NONE(), Absyn.dummyInfo);
56+
57+
// Default Integer parameter.
58+
constant Component INT_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
59+
Type.INTEGER(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);
60+
61+
constant InstNode INT_PARAM = InstNode.COMPONENT_NODE("i",
62+
DUMMY_ELEMENT, listArray({INT_COMPONENT}), InstNode.EMPTY_NODE());
63+
64+
// Default Real parameter.
65+
constant Component REAL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
66+
Type.REAL(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);
67+
68+
constant InstNode REAL_PARAM = InstNode.COMPONENT_NODE("r",
69+
DUMMY_ELEMENT, listArray({REAL_COMPONENT}), InstNode.EMPTY_NODE());
70+
71+
// Default Boolean parameter.
72+
constant Component BOOL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
73+
Type.BOOLEAN(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);
74+
75+
constant InstNode BOOL_PARAM = InstNode.COMPONENT_NODE("b",
76+
DUMMY_ELEMENT, listArray({BOOL_COMPONENT}), InstNode.EMPTY_NODE());
77+
78+
// Default String parameter.
79+
constant Component STRING_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
80+
Type.STRING(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);
81+
82+
constant InstNode STRING_PARAM = InstNode.COMPONENT_NODE("s",
83+
DUMMY_ELEMENT, listArray({STRING_COMPONENT}), InstNode.EMPTY_NODE());
84+
85+
// Default enumeration(:) parameter.
86+
constant Component ENUM_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
87+
Type.ENUMERATION_ANY(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);
88+
89+
constant InstNode ENUM_PARAM = InstNode.COMPONENT_NODE("e",
90+
DUMMY_ELEMENT, listArray({ENUM_COMPONENT}), InstNode.EMPTY_NODE());
91+
92+
// Integer(e)
93+
constant Function INTEGER = Function.FUNCTION(Path.IDENT("Integer"),
94+
InstNode.EMPTY_NODE(), {ENUM_PARAM}, {}, {}, {
95+
Slot.SLOT("e", SlotType.POSITIONAL, NONE(), NONE())
96+
}, Type.INTEGER(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));
97+
98+
// String(r, significantDigits=d, minimumLength=0, leftJustified=true)
99+
constant InstNode STRING_NODE = NFInstNode.CLASS_NODE("String", DUMMY_ELEMENT,
100+
listArray({}), listArray({}), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_CLASS());
101+
102+
constant Function STRING_REAL = Function.FUNCTION(Path.IDENT("String"),
103+
STRING_NODE, {REAL_PARAM, INT_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
104+
Slot.SLOT("r", SlotType.POSITIONAL, NONE(), NONE()),
105+
Slot.SLOT("significantDigits", SlotType.NAMED, SOME(Expression.INTEGER(6)), NONE()),
106+
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
107+
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
108+
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));
109+
110+
// String(r, format="-0.6g")
111+
constant Function STRING_REAL_FORMAT = Function.FUNCTION(Path.IDENT("String"),
112+
STRING_NODE, {REAL_PARAM, STRING_PARAM}, {STRING_PARAM}, {}, {
113+
Slot.SLOT("r", SlotType.POSITIONAL, NONE(), NONE()),
114+
Slot.SLOT("format", SlotType.NAMED, SOME(Expression.STRING("-0.6g")), NONE())
115+
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));
116+
117+
// String(i, minimumLength=0, leftJustified=true)
118+
constant Function STRING_INT = Function.FUNCTION(Path.IDENT("String"),
119+
STRING_NODE, {INT_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
120+
Slot.SLOT("i", SlotType.POSITIONAL, NONE(), NONE()),
121+
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
122+
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
123+
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));
124+
125+
// String(b, minimumLength=0, leftJustified=true)
126+
constant Function STRING_BOOL = Function.FUNCTION(Path.IDENT("String"),
127+
STRING_NODE, {BOOL_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
128+
Slot.SLOT("b", SlotType.POSITIONAL, NONE(), NONE()),
129+
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
130+
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
131+
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));
132+
133+
// String(e, minimumLength=0, leftJustified=true)
134+
constant Function STRING_ENUM = Function.FUNCTION(Path.IDENT("String"),
135+
STRING_NODE, {ENUM_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
136+
Slot.SLOT("e", SlotType.POSITIONAL, NONE(), NONE()),
137+
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
138+
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
139+
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));
140+
141+
annotation(__OpenModelica_Interface="frontend");
142+
end NFBuiltinFuncs;

0 commit comments

Comments
 (0)