Skip to content

Commit

Permalink
[NF] Improved handling of types.
Browse files Browse the repository at this point in the history
- Added class trees to the builtin types with their attributes, to
  simplify the handling of builtin attributes and make inheriting
  builtin types work properly.
- Changed instantiation of short class declarations so that we keep
  the derived class instead of replacing it with the base class,
  since we otherwise loose information we need later.
- Changed the instantiation of components so that dimensions from
  their types are added during typing instead of during instantiation.
- Various other fixes.

Belonging to [master]:
  - OpenModelica/OMCompiler#2383
  - OpenModelica/OpenModelica-testsuite#928
  • Loading branch information
perost authored and OpenModelica-Hudson committed Apr 20, 2018
1 parent 29bc2a8 commit c9c4c30
Show file tree
Hide file tree
Showing 15 changed files with 877 additions and 614 deletions.
183 changes: 178 additions & 5 deletions Compiler/NFFrontEnd/NFBuiltin.mo
Expand Up @@ -60,6 +60,8 @@ import NFPrefixes.Visibility;
import ComponentRef = NFComponentRef;
import NFComponentRef.Origin;
import Restriction = NFRestriction;
import NFClassTree.LookupTree;
import NFClassTree.DuplicateTree;

protected
import MetaModelica.Dangerous.*;
Expand Down Expand Up @@ -124,36 +126,181 @@ constant InstNode POLYMORPHIC_NODE = InstNode.CLASS_NODE("polymorphic",
Modifier.NOMOD(), Restriction.TYPE())),
EMPTY_NODE_CACHE, InstNode.EMPTY_NODE(), InstNodeType.BUILTIN_CLASS());

// Lookup tree for Real. Generated by makeBuiltinLookupTree.
constant LookupTree.Tree REAL_LOOKUP_TREE = LookupTree.Tree.NODE(
key = "quantity", value = LookupTree.Entry.COMPONENT(index = 1), height = 4,
left = NFClassTree.LookupTree.Tree.NODE(
key = "max", value = LookupTree.Entry.COMPONENT(index = 5), height = 3,
left = LookupTree.Tree.NODE(
key = "displayUnit", value = NFClassTree.LookupTree.Entry.COMPONENT(index = 3), height = 2,
left = LookupTree.Tree.EMPTY(),
right = LookupTree.Tree.LEAF(
key = "fixed", value = LookupTree.Entry.COMPONENT(index = 7))),
right = LookupTree.Tree.NODE(
key = "min", value = LookupTree.Entry.COMPONENT(index = 4), height = 2,
left = LookupTree.Tree.EMPTY(),
right = LookupTree.Tree.LEAF(
key = "nominal", value = LookupTree.Entry.COMPONENT(index = 8)))),
right = LookupTree.Tree.NODE(
key = "unbounded", value = LookupTree.Entry.COMPONENT(index = 9), height = 3,
left = LookupTree.Tree.NODE(
key = "start", value = LookupTree.Entry.COMPONENT(index = 6), height = 2,
left = LookupTree.Tree.EMPTY(),
right = LookupTree.Tree.LEAF(
key = "stateSelect", value = LookupTree.Entry.COMPONENT(index = 10))),
right = LookupTree.Tree.LEAF(
key = "unit", value = LookupTree.Entry.COMPONENT(index = 2))));

constant ClassTree REAL_CLASS_TREE = ClassTree.FLAT_TREE(
REAL_LOOKUP_TREE,
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("unit", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("displayUnit", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("min", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("max", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("nominal", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.REAL(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("unbounded", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("stateSelect", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(STATESELECT_TYPE, Modifier.NOMOD())), InstNode.EMPTY_NODE())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());

constant InstNode REAL_NODE = InstNode.CLASS_NODE("Real",
Elements.REAL, Visibility.PUBLIC,
Pointer.createImmutable(
Class.PARTIAL_BUILTIN(Type.REAL(), ClassTree.EMPTY_TREE(), Modifier.NOMOD(), Restriction.TYPE())),
Class.PARTIAL_BUILTIN(Type.REAL(), REAL_CLASS_TREE, Modifier.NOMOD(), Restriction.TYPE())),
EMPTY_NODE_CACHE, InstNode.EMPTY_NODE(), InstNodeType.BUILTIN_CLASS());

// Lookup tree for Integer. Generated by makeBuiltinLookupTree.
constant LookupTree.Tree INTEGER_LOOKUP_TREE = LookupTree.Tree.NODE(
key = "min", value = LookupTree.Entry.COMPONENT(index = 2), height = 3,
left = LookupTree.Tree.NODE(
key = "max", value = LookupTree.Entry.COMPONENT(index = 3), height = 2,
left = LookupTree.Tree.LEAF(
key = "fixed", value = LookupTree.Entry.COMPONENT(index = 5)),
right = LookupTree.Tree.EMPTY()),
right = LookupTree.Tree.NODE(
key = "quantity", value = LookupTree.Entry.COMPONENT(index = 1), height = 2,
left = LookupTree.Tree.EMPTY(),
right = LookupTree.Tree.LEAF(
key = "start", value = LookupTree.Entry.COMPONENT(index = 4))));

constant ClassTree INTEGER_CLASS_TREE = ClassTree.FLAT_TREE(
INTEGER_LOOKUP_TREE,
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("min", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("max", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.INTEGER(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());

constant InstNode INTEGER_NODE = InstNode.CLASS_NODE("Integer",
Elements.INTEGER, Visibility.PUBLIC,
Pointer.createImmutable(
Class.PARTIAL_BUILTIN(Type.INTEGER(), ClassTree.EMPTY_TREE(), Modifier.NOMOD(), Restriction.TYPE())),
Class.PARTIAL_BUILTIN(Type.INTEGER(), INTEGER_CLASS_TREE, Modifier.NOMOD(), Restriction.TYPE())),
EMPTY_NODE_CACHE, InstNode.EMPTY_NODE(), InstNodeType.BUILTIN_CLASS());

// Lookup tree for Boolean. Generated by makeBuiltinLookupTree.
constant LookupTree.Tree BOOLEAN_LOOKUP_TREE = LookupTree.Tree.NODE(
key = "quantity", value = LookupTree.Entry.COMPONENT(index = 1), height = 2,
left = LookupTree.Tree.LEAF(
key = "fixed", value = LookupTree.Entry.COMPONENT(index = 3)),
right = LookupTree.Tree.LEAF(
key = "start", value = LookupTree.Entry.COMPONENT(index = 2)));

constant ClassTree BOOLEAN_CLASS_TREE = ClassTree.FLAT_TREE(
BOOLEAN_LOOKUP_TREE,
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());

constant InstNode BOOLEAN_NODE = InstNode.CLASS_NODE("Boolean",
Elements.BOOLEAN, Visibility.PUBLIC,
Pointer.createImmutable(
Class.PARTIAL_BUILTIN(Type.BOOLEAN(), ClassTree.EMPTY_TREE(), Modifier.NOMOD(), Restriction.TYPE())),
Class.PARTIAL_BUILTIN(Type.BOOLEAN(), BOOLEAN_CLASS_TREE, Modifier.NOMOD(), Restriction.TYPE())),
EMPTY_NODE_CACHE, InstNode.EMPTY_NODE(), InstNodeType.BUILTIN_CLASS());

constant ComponentRef BOOLEAN_CREF =
ComponentRef.CREF(BOOLEAN_NODE, {}, Type.INTEGER(), Origin.CREF, ComponentRef.EMPTY());

// Lookup tree for String. Generated by makeBuiltinLookupTree.
constant LookupTree.Tree STRING_LOOKUP_TREE = LookupTree.Tree.NODE(
key = "quantity", value = LookupTree.Entry.COMPONENT(index = 1), height = 2,
left = LookupTree.Tree.LEAF(
key = "fixed", value = LookupTree.Entry.COMPONENT(index = 3)),
right = LookupTree.Tree.LEAF(
key = "start", value = LookupTree.Entry.COMPONENT(index = 2)));

constant ClassTree STRING_CLASS_TREE = ClassTree.FLAT_TREE(
STRING_LOOKUP_TREE,
listArrayLiteral({}),
listArrayLiteral({
InstNode.COMPONENT_NODE("quantity", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("start", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.STRING(), Modifier.NOMOD())), InstNode.EMPTY_NODE()),
InstNode.COMPONENT_NODE("fixed", Visibility.PUBLIC,
Pointer.createImmutable(Component.TYPE_ATTRIBUTE(Type.BOOLEAN(), Modifier.NOMOD())), InstNode.EMPTY_NODE())
}),
listArray({}), // TODO: #4895: This should be listArrayLiteral too, but causes compilation issues.
DuplicateTree.EMPTY());

constant InstNode STRING_NODE = InstNode.CLASS_NODE("String",
Elements.STRING, Visibility.PUBLIC,
Pointer.createImmutable(
Class.PARTIAL_BUILTIN(Type.STRING(), ClassTree.EMPTY_TREE(), Modifier.NOMOD(), Restriction.TYPE())),
Class.PARTIAL_BUILTIN(Type.STRING(), STRING_CLASS_TREE, Modifier.NOMOD(), Restriction.TYPE())),
EMPTY_NODE_CACHE, InstNode.EMPTY_NODE(), InstNodeType.BUILTIN_CLASS());

// Lookup tree for enumerations. Generated by makeBuiltinLookupTree.
// NOTE: The enumeration attributes themselves are created by ClassTree.fromEnumeration,
// so any changes to this lookup tree requires fromEnumeration to be updated too.
constant LookupTree.Tree ENUM_LOOKUP_TREE = LookupTree.Tree.NODE(
key = "min", value = LookupTree.Entry.COMPONENT(index = 2), height = 3,
left = LookupTree.Tree.NODE(
key = "max", value = LookupTree.Entry.COMPONENT(index = 3), height = 2,
left = LookupTree.Tree.LEAF(
key = "fixed", value = LookupTree.Entry.COMPONENT(index = 5)),
right = LookupTree.Tree.EMPTY()),
right = LookupTree.Tree.NODE(
key = "quantity", value = LookupTree.Entry.COMPONENT(index = 1), height = 2,
left = LookupTree.Tree.EMPTY(),
right = LookupTree.Tree.LEAF(
key = "start", value = LookupTree.Entry.COMPONENT(index = 4))));

constant InstNode ENUM_NODE = InstNode.CLASS_NODE("enumeration",
Elements.ENUMERATION, Visibility.PUBLIC,
Pointer.createImmutable(Class.PARTIAL_BUILTIN(Type.ENUMERATION_ANY(), ClassTree.EMPTY_TREE(),
Pointer.createImmutable(Class.PARTIAL_BUILTIN(Type.ENUMERATION_ANY(), NFClassTree.EMPTY_TREE(),
Modifier.NOMOD(), Restriction.ENUMERATION())),
EMPTY_NODE_CACHE, InstNode.EMPTY_NODE(), InstNodeType.BUILTIN_CLASS());

Expand Down Expand Up @@ -184,5 +331,31 @@ constant InstNode TIME =

constant ComponentRef TIME_CREF = ComponentRef.CREF(TIME, {}, Type.REAL(), Origin.CREF, ComponentRef.EMPTY());

function makeBuiltinLookupTree
"This function takes lists of component and class names and prints out a lookup tree.
Useful in case any attributes needs to be added to any of the builtin type."
input String name "Not used in the tree, only to identify the printout.";
input list<String> components;
input list<String> classes = {};
protected
LookupTree.Tree ltree = LookupTree.new();
Integer i;
algorithm
i := 1;
for comp in components loop
ltree := LookupTree.add(ltree, comp, LookupTree.Entry.COMPONENT(i));
i := i + 1;
end for;

for cls in classes loop
ltree := LookupTree.add(ltree, cls, LookupTree.Entry.COMPONENT(i));
i := i + 1;
end for;

print("Lookup tree for " + name + ":\n");
print(anyString(ltree));
print("\n");
end makeBuiltinLookupTree;

annotation(__OpenModelica_Interface="frontend");
end NFBuiltin;
4 changes: 2 additions & 2 deletions Compiler/NFFrontEnd/NFBuiltinFuncs.mo
Expand Up @@ -132,8 +132,8 @@ constant Function INTEGER_FUNCTION = Function.FUNCTION(Path.IDENT("Integer"),

constant InstNode INTEGER_NODE = InstNode.CLASS_NODE("IntegerFunc",
DUMMY_ELEMENT, Visibility.PUBLIC,
Pointer.createImmutable(Class.INSTANCED_CLASS(ClassTree.EMPTY_TREE(),
Sections.EMPTY(), Type.UNKNOWN(), Restriction.FUNCTION())),
Pointer.createImmutable(Class.INSTANCED_CLASS(Type.UNKNOWN(), ClassTree.EMPTY_TREE(),
Sections.EMPTY(), Restriction.FUNCTION())),
listArrayLiteral({NFInstNode.CachedData.FUNCTION({INTEGER_FUNCTION}, true, false),
NFInstNode.CachedData.NO_CACHE(),
NFInstNode.CachedData.NO_CACHE()}),
Expand Down

0 comments on commit c9c4c30

Please sign in to comment.