Skip to content

Commit

Permalink
Improved function handling in NFInst.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
perost authored and OpenModelica-Hudson committed Mar 8, 2017
1 parent 2a857f1 commit 4c362d1
Show file tree
Hide file tree
Showing 23 changed files with 1,399 additions and 498 deletions.
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFBinding.mo
Expand Up @@ -32,7 +32,7 @@
encapsulated uniontype NFBinding
public
import DAE;
import NFExpression.Expression;
import Expression = NFExpression;
import NFInstNode.InstNode;
import SCode;
import Type = NFType;
Expand Down
26 changes: 20 additions & 6 deletions Compiler/NFFrontEnd/NFBuiltin.mo
Expand Up @@ -48,11 +48,12 @@ import Binding = NFBinding;
import NFClass.Class;
import NFClass.ClassTree;
import NFComponent.Component;
import NFExpression.Expression;
import Expression = NFExpression;
import NFInstNode.InstNode;
import NFInstNode.InstNodeType;
import NFMod.Modifier;
import Type = NFType;
import BuiltinFuncs = NFBuiltinFuncs;

encapsulated package Elements
import SCode;
Expand Down Expand Up @@ -86,23 +87,27 @@ encapsulated package Elements
constant SCode.Element REAL = SCode.CLASS("Real",
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
SCode.noComment,Absyn.dummyInfo);
SCode.noComment, Absyn.dummyInfo);

constant SCode.Element INTEGER = SCode.CLASS("Integer",
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
SCode.noComment,Absyn.dummyInfo);
SCode.noComment, Absyn.dummyInfo);

constant SCode.Element BOOLEAN = SCode.CLASS("Boolean",
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
SCode.noComment,Absyn.dummyInfo);
SCode.noComment, Absyn.dummyInfo);

constant SCode.Element STRING = SCode.CLASS("String",
SCode.defaultPrefixes, SCode.NOT_ENCAPSULATED(), SCode.NOT_PARTIAL(), SCode.R_TYPE(),
SCode.PARTS({}, {}, {}, {}, {}, {}, {}, NONE()),
SCode.noComment,Absyn.dummyInfo);
SCode.noComment, Absyn.dummyInfo);

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

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

constant InstNode BOOLEAN_TYPE = InstNode.CLASS_NODE("Boolean",
Expand All @@ -146,6 +151,15 @@ constant InstNode BOOLEAN_TYPE = InstNode.CLASS_NODE("Boolean",
constant InstNode STRING_TYPE = InstNode.CLASS_NODE("String",
Elements.STRING,
listArray({Class.PARTIAL_BUILTIN(Type.STRING(), ClassTree.EMPTY(), listArray({}), Modifier.NOMOD())}),
listArray({NFInstNode.CachedData.FUNCTION({
NFBuiltinFuncs.STRING_ENUM, NFBuiltinFuncs.STRING_INT,
NFBuiltinFuncs.STRING_BOOL, NFBuiltinFuncs.STRING_REAL,
NFBuiltinFuncs.STRING_REAL_FORMAT}, true)}),
InstNode.EMPTY_NODE(), InstNodeType.NORMAL_CLASS());

constant InstNode ENUM_TYPE = InstNode.CLASS_NODE("enumeration",
Elements.ENUMERATION,
listArray({Class.PARTIAL_BUILTIN(Type.ENUMERATION_ANY(), ClassTree.EMPTY(), listArray({}), Modifier.NOMOD())}),
listArray({NFInstNode.CachedData.NO_CACHE()}),
InstNode.EMPTY_NODE(), InstNodeType.NORMAL_CLASS());

Expand Down
142 changes: 142 additions & 0 deletions Compiler/NFFrontEnd/NFBuiltinFuncs.mo
@@ -0,0 +1,142 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, 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 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, 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 NFBuiltinFuncs

import NFFunction.Function;
import NFFunction.Slot;
import NFFunction.SlotType;
import NFFunction.FuncType;
import NFInstNode.InstNode;
import NFInstNode.InstNodeType;
import NFComponent.Component;
import Type = NFType;
import Expression = NFExpression;
import Absyn;
import Absyn.{Path, TypeSpec};
import SCode;
import SCode.{Mod, Comment};
import DAE;
import Builtin = NFBuiltin;
import Binding = NFBinding;

// Dummy SCode component, since we usually don't need the definition for anything.
constant SCode.Element DUMMY_ELEMENT = SCode.COMPONENT("dummy",
SCode.defaultPrefixes, SCode.defaultVarAttr,
TypeSpec.TPATH(Path.IDENT("$dummy"), NONE()), SCode.Mod.NOMOD(),
SCode.Comment.COMMENT(NONE(), NONE()), NONE(), Absyn.dummyInfo);

// Default Integer parameter.
constant Component INT_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.INTEGER(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);

constant InstNode INT_PARAM = InstNode.COMPONENT_NODE("i",
DUMMY_ELEMENT, listArray({INT_COMPONENT}), InstNode.EMPTY_NODE());

// Default Real parameter.
constant Component REAL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.REAL(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);

constant InstNode REAL_PARAM = InstNode.COMPONENT_NODE("r",
DUMMY_ELEMENT, listArray({REAL_COMPONENT}), InstNode.EMPTY_NODE());

// Default Boolean parameter.
constant Component BOOL_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.BOOLEAN(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);

constant InstNode BOOL_PARAM = InstNode.COMPONENT_NODE("b",
DUMMY_ELEMENT, listArray({BOOL_COMPONENT}), InstNode.EMPTY_NODE());

// Default String parameter.
constant Component STRING_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.STRING(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);

constant InstNode STRING_PARAM = InstNode.COMPONENT_NODE("s",
DUMMY_ELEMENT, listArray({STRING_COMPONENT}), InstNode.EMPTY_NODE());

// Default enumeration(:) parameter.
constant Component ENUM_COMPONENT = Component.TYPED_COMPONENT(NFInstNode.EMPTY_NODE(),
Type.ENUMERATION_ANY(), Binding.UNBOUND(), NFComponent.DEFAULT_ATTR);

constant InstNode ENUM_PARAM = InstNode.COMPONENT_NODE("e",
DUMMY_ELEMENT, listArray({ENUM_COMPONENT}), InstNode.EMPTY_NODE());

// Integer(e)
constant Function INTEGER = Function.FUNCTION(Path.IDENT("Integer"),
InstNode.EMPTY_NODE(), {ENUM_PARAM}, {}, {}, {
Slot.SLOT("e", SlotType.POSITIONAL, NONE(), NONE())
}, Type.INTEGER(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));

// String(r, significantDigits=d, minimumLength=0, leftJustified=true)
constant InstNode STRING_NODE = NFInstNode.CLASS_NODE("String", DUMMY_ELEMENT,
listArray({}), listArray({}), InstNode.EMPTY_NODE(), InstNodeType.NORMAL_CLASS());

constant Function STRING_REAL = Function.FUNCTION(Path.IDENT("String"),
STRING_NODE, {REAL_PARAM, INT_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
Slot.SLOT("r", SlotType.POSITIONAL, NONE(), NONE()),
Slot.SLOT("significantDigits", SlotType.NAMED, SOME(Expression.INTEGER(6)), NONE()),
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));

// String(r, format="-0.6g")
constant Function STRING_REAL_FORMAT = Function.FUNCTION(Path.IDENT("String"),
STRING_NODE, {REAL_PARAM, STRING_PARAM}, {STRING_PARAM}, {}, {
Slot.SLOT("r", SlotType.POSITIONAL, NONE(), NONE()),
Slot.SLOT("format", SlotType.NAMED, SOME(Expression.STRING("-0.6g")), NONE())
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));

// String(i, minimumLength=0, leftJustified=true)
constant Function STRING_INT = Function.FUNCTION(Path.IDENT("String"),
STRING_NODE, {INT_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
Slot.SLOT("i", SlotType.POSITIONAL, NONE(), NONE()),
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));

// String(b, minimumLength=0, leftJustified=true)
constant Function STRING_BOOL = Function.FUNCTION(Path.IDENT("String"),
STRING_NODE, {BOOL_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
Slot.SLOT("b", SlotType.POSITIONAL, NONE(), NONE()),
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));

// String(e, minimumLength=0, leftJustified=true)
constant Function STRING_ENUM = Function.FUNCTION(Path.IDENT("String"),
STRING_NODE, {ENUM_PARAM, INT_PARAM, BOOL_PARAM}, {STRING_PARAM}, {}, {
Slot.SLOT("e", SlotType.POSITIONAL, NONE(), NONE()),
Slot.SLOT("minimumLength", SlotType.NAMED, SOME(Expression.INTEGER(0)), NONE()),
Slot.SLOT("leftJustified", SlotType.NAMED, SOME(Expression.BOOLEAN(true)), NONE())
}, Type.STRING(), DAE.FUNCTION_ATTRIBUTES_BUILTIN, listArray({true}));

annotation(__OpenModelica_Interface="frontend");
end NFBuiltinFuncs;

0 comments on commit 4c362d1

Please sign in to comment.