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

Commit 803cabd

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Record fixes.
- Generate default record constructors even when they aren't explicitly called, since calls might be generated by the backend. - Remove the 'constructor'.$default suffix that was previously added to generated record constructors, since the rest of the compiler assumes constructors have the same name as the record itself. - Use DAE.Function.RECORD_CONSTRUCTOR for record constructors instead of DAE.Function.FUNCTION. Belonging to [master]: - #2437 - OpenModelica/OpenModelica-testsuite#946
1 parent 57e8ce7 commit 803cabd

File tree

10 files changed

+193
-99
lines changed

10 files changed

+193
-99
lines changed

Compiler/NFFrontEnd/NFCall.mo

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,6 @@ uniontype Call
194194
then
195195
fail();
196196
end match;
197-
198-
// callExp := Expression.CALL(call);
199-
200197
end instantiate;
201198

202199
protected
@@ -589,7 +586,7 @@ uniontype Call
589586

590587
// Type the function(s) if not already done.
591588
if not typed then
592-
functions := list(Function.typeFunction(f) for f in functions);
589+
functions := list(Function.typeFunctionSignature(f) for f in functions);
593590
InstNode.setFuncCache(fn_node, CachedData.FUNCTION(functions, true, special));
594591
functions := list(Function.typeFunctionBody(f) for f in functions);
595592
InstNode.setFuncCache(fn_node, CachedData.FUNCTION(functions, true, special));
@@ -979,18 +976,11 @@ uniontype Call
979976
input list<MatchedFunction> matchedFunctions;
980977
output list<MatchedFunction> outMatches;
981978
algorithm
982-
outMatches := {};
983979
// We have at least two exact matches. find the default constructor (if there is one) and remove it from the list
984980
// so that it
985981
// - doesn't cause ambiguities if there is only one other match left OR
986982
// - it doesn't appear in the error messages in the case of more than one overloaded constructor matches.
987-
for mt_fn in matchedFunctions loop
988-
if not stringEqual(Absyn.pathLastIdent(mt_fn.func.path), "'constructor'.'$default'") then
989-
outMatches := mt_fn::outMatches;
990-
end if;
991-
end for;
992-
993-
outMatches := listReverse(outMatches);
983+
outMatches := list(m for m guard not Function.isDefaultRecordConstructor(m.func) in matchedFunctions);
994984
end resolveOverloadedVsDefaultConstructorAmbigutiy;
995985

996986
function typeOf
@@ -2696,7 +2686,7 @@ protected
26962686
algorithm
26972687
exp := match call
26982688
case TYPED_CALL()
2699-
then Expression.RECORD(Absyn.stripLast(Function.name(call.fn)), ty, call.arguments);
2689+
then Expression.RECORD(Function.name(call.fn), ty, call.arguments);
27002690
end match;
27012691
end toRecordExpression;
27022692

Compiler/NFFrontEnd/NFClass.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ uniontype Class
163163
ClassTree tree;
164164
algorithm
165165
tree := ClassTree.fromRecordConstructor(inputs, locals, out);
166-
cls := INSTANCED_CLASS(Type.UNKNOWN(), tree, Sections.EMPTY(), Restriction.FUNCTION());
166+
cls := INSTANCED_CLASS(Type.UNKNOWN(), tree, Sections.EMPTY(), Restriction.RECORD_CONSTRUCTOR());
167167
end makeRecordConstructor;
168168

169169
function initExpandedClass

Compiler/NFFrontEnd/NFComplexType.mo

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public
5353
Boolean isExpandable;
5454
end CONNECTOR;
5555

56+
record RECORD
57+
InstNode constructor;
58+
end RECORD;
59+
5660
record EXTERNAL_OBJECT
5761
InstNode constructor;
5862
InstNode destructor;

Compiler/NFFrontEnd/NFConvertDAE.mo

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import FlatModel = NFFlatModel;
3838
import NFFlatten.FunctionTree;
3939
import NFInstNode.InstNode;
4040
import Statement = NFStatement;
41+
import Restriction = NFRestriction;
4142

4243
protected
4344
import ExecStat.execStat;
@@ -911,7 +912,7 @@ algorithm
911912
cls := InstNode.getClass(Function.instance(func));
912913

913914
dfunc := match cls
914-
case Class.INSTANCED_CLASS(sections = sections)
915+
case Class.INSTANCED_CLASS(sections = sections, restriction = Restriction.FUNCTION())
915916
algorithm
916917
elems := convertFunctionParams(func.inputs, {});
917918
elems := convertFunctionParams(func.outputs, elems);
@@ -935,6 +936,11 @@ algorithm
935936
then
936937
Function.toDAE(func, {def});
937938

939+
case Class.INSTANCED_CLASS(restriction = Restriction.RECORD_CONSTRUCTOR())
940+
then DAE.Function.RECORD_CONSTRUCTOR(Function.name(func),
941+
Function.makeDAEType(func),
942+
DAE.emptyElementSource);
943+
938944
else
939945
algorithm
940946
Error.assertion(false, getInstanceName() + " got unknown function", sourceInfo());

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -852,30 +852,49 @@ function collectTypeFuncs
852852
input output FunctionTree funcs;
853853
algorithm
854854
() := match ty
855+
local
856+
InstNode con, de;
857+
855858
// Collect external object structors.
856-
case Type.COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT())
859+
case Type.COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT(constructor = con, destructor = de))
860+
algorithm
861+
funcs := collectStructor(con, funcs);
862+
funcs := collectStructor(de, funcs);
863+
then
864+
();
865+
866+
// Collect record constructors.
867+
case Type.COMPLEX(complexTy = ComplexType.RECORD(constructor = con))
857868
algorithm
858-
funcs := collectExternalObjectStructors(ty.complexTy, funcs);
869+
funcs := collectStructor(con, funcs);
859870
then
860871
();
861872

862873
else ();
863874
end match;
864875
end collectTypeFuncs;
865876

866-
function collectExternalObjectStructors
867-
input ComplexType ty;
877+
function collectStructor
878+
input InstNode node;
868879
input output FunctionTree funcs;
869880
protected
870-
InstNode constructor, destructor;
871-
Function fn;
881+
CachedData cache;
882+
list<Function> fn;
872883
algorithm
873-
ComplexType.EXTERNAL_OBJECT(constructor, destructor) := ty;
874-
CachedData.FUNCTION(funcs = {fn}) := InstNode.getFuncCache(constructor);
875-
funcs := flattenFunction(fn, funcs);
876-
CachedData.FUNCTION(funcs = {fn}) := InstNode.getFuncCache(destructor);
877-
funcs := flattenFunction(fn, funcs);
878-
end collectExternalObjectStructors;
884+
cache := InstNode.getFuncCache(node);
885+
886+
() := match cache
887+
case CachedData.FUNCTION()
888+
algorithm
889+
for fn in cache.funcs loop
890+
funcs := flattenFunction(fn, funcs);
891+
end for;
892+
then
893+
();
894+
895+
else ();
896+
end match;
897+
end collectStructor;
879898

880899
function collectEquationFuncs
881900
input Equation eq;

Compiler/NFFrontEnd/NFFunction.mo

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,14 @@ uniontype Function
293293
protected
294294
CachedData cache;
295295
algorithm
296-
// Look up the the function.
296+
// Look up the function.
297297
fn_node := ComponentRef.node(fn_ref);
298298
cache := InstNode.getFuncCache(fn_node);
299299

300300
// Check if a cached instantiation of this function already exists.
301301
(fn_node, specialBuiltin) := match cache
302302
case CachedData.FUNCTION() then (fn_node, cache.specialBuiltin);
303-
else algorithm
304-
(fn_node, specialBuiltin) := instFunc2(ComponentRef.toPath(fn_ref), fn_node, info);
305-
//instFuncExpressions(fn_node);
306-
then (fn_node, specialBuiltin);
303+
else instFunc2(ComponentRef.toPath(fn_ref), fn_node, info);
307304
end match;
308305
end instFuncRef;
309306

@@ -945,6 +942,13 @@ uniontype Function
945942
end isTyped;
946943

947944
function typeFunction
945+
input output Function fn;
946+
algorithm
947+
fn := typeFunctionSignature(fn);
948+
fn := typeFunctionBody(fn);
949+
end typeFunction;
950+
951+
function typeFunctionSignature
948952
"Types a function's parameters, local components and default arguments."
949953
input output Function fn;
950954
protected
@@ -969,7 +973,7 @@ uniontype Function
969973
checkParamTypes(fn);
970974
fn.returnType := makeReturnType(fn);
971975
end if;
972-
end typeFunction;
976+
end typeFunctionSignature;
973977

974978
function typeFunctionBody
975979
"Types the body of a function, along with any bindings of local variables
@@ -1113,6 +1117,16 @@ uniontype Function
11131117
end match;
11141118
end inlineBuiltin;
11151119

1120+
function isDefaultRecordConstructor
1121+
input Function fn;
1122+
output Boolean isConstructor;
1123+
algorithm
1124+
isConstructor := match Class.restriction(InstNode.getClass(fn.node))
1125+
case Restriction.RECORD_CONSTRUCTOR() then true;
1126+
else false;
1127+
end match;
1128+
end isDefaultRecordConstructor;
1129+
11161130
function toDAE
11171131
input Function fn;
11181132
input list<DAE.FunctionDefinition> defs;

Compiler/NFFrontEnd/NFInst.mo

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ import FlatModel = NFFlatModel;
9595
import BindingOrigin = NFBindingOrigin;
9696
import ElementSource;
9797
import SimplifyModel = NFSimplifyModel;
98+
import Record = NFRecord;
9899

99100
type EquationScope = enumeration(NORMAL, INITIAL, WHEN);
100101

@@ -1784,9 +1785,11 @@ algorithm
17841785
// Instantiate local equation/algorithm sections.
17851786
sections := instSections(node, scope, sections);
17861787

1787-
ty := Type.COMPLEX(node, ComplexType.CLASS());
1788+
ty := makeComplexType(cls.restriction, node);
17881789
inst_cls := Class.INSTANCED_CLASS(ty, cls.elements, sections, cls.restriction);
17891790
InstNode.updateClass(inst_cls, node);
1791+
1792+
instComplexType(ty);
17901793
then
17911794
();
17921795

@@ -1823,6 +1826,50 @@ algorithm
18231826
end match;
18241827
end instExpressions;
18251828

1829+
function makeComplexType
1830+
input Restriction restriction;
1831+
input InstNode node;
1832+
output Type ty;
1833+
protected
1834+
ComplexType cty;
1835+
algorithm
1836+
cty := match restriction
1837+
case Restriction.RECORD() then ComplexType.RECORD(node);
1838+
else ComplexType.CLASS();
1839+
end match;
1840+
1841+
ty := Type.COMPLEX(node, cty);
1842+
end makeComplexType;
1843+
1844+
function instComplexType
1845+
input Type ty;
1846+
algorithm
1847+
() := match ty
1848+
local
1849+
InstNode node;
1850+
CachedData cache;
1851+
1852+
case Type.COMPLEX(complexTy = ComplexType.RECORD(node))
1853+
algorithm
1854+
cache := InstNode.getFuncCache(node);
1855+
1856+
() := match cache
1857+
case CachedData.FUNCTION() then ();
1858+
else
1859+
algorithm
1860+
InstNode.cacheInitFunc(node);
1861+
Record.instConstructors(InstNode.scopePath(node), node, InstNode.info(node));
1862+
then
1863+
();
1864+
1865+
end match;
1866+
then
1867+
();
1868+
1869+
else ();
1870+
end match;
1871+
end instComplexType;
1872+
18261873
function instBuiltinAttribute
18271874
input output Modifier attribute;
18281875
algorithm

0 commit comments

Comments
 (0)