Skip to content

Commit

Permalink
[NF] Record fixes.
Browse files Browse the repository at this point in the history
- 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]:
  - OpenModelica/OMCompiler#2437
  - OpenModelica/OpenModelica-testsuite#946
  • Loading branch information
perost authored and OpenModelica-Hudson committed May 16, 2018
1 parent 57e8ce7 commit 803cabd
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 99 deletions.
16 changes: 3 additions & 13 deletions Compiler/NFFrontEnd/NFCall.mo
Expand Up @@ -194,9 +194,6 @@ uniontype Call
then
fail();
end match;

// callExp := Expression.CALL(call);

end instantiate;

protected
Expand Down Expand Up @@ -589,7 +586,7 @@ uniontype Call

// Type the function(s) if not already done.
if not typed then
functions := list(Function.typeFunction(f) for f in functions);
functions := list(Function.typeFunctionSignature(f) for f in functions);
InstNode.setFuncCache(fn_node, CachedData.FUNCTION(functions, true, special));
functions := list(Function.typeFunctionBody(f) for f in functions);
InstNode.setFuncCache(fn_node, CachedData.FUNCTION(functions, true, special));
Expand Down Expand Up @@ -979,18 +976,11 @@ uniontype Call
input list<MatchedFunction> matchedFunctions;
output list<MatchedFunction> outMatches;
algorithm
outMatches := {};
// We have at least two exact matches. find the default constructor (if there is one) and remove it from the list
// so that it
// - doesn't cause ambiguities if there is only one other match left OR
// - it doesn't appear in the error messages in the case of more than one overloaded constructor matches.
for mt_fn in matchedFunctions loop
if not stringEqual(Absyn.pathLastIdent(mt_fn.func.path), "'constructor'.'$default'") then
outMatches := mt_fn::outMatches;
end if;
end for;

outMatches := listReverse(outMatches);
outMatches := list(m for m guard not Function.isDefaultRecordConstructor(m.func) in matchedFunctions);
end resolveOverloadedVsDefaultConstructorAmbigutiy;

function typeOf
Expand Down Expand Up @@ -2696,7 +2686,7 @@ protected
algorithm
exp := match call
case TYPED_CALL()
then Expression.RECORD(Absyn.stripLast(Function.name(call.fn)), ty, call.arguments);
then Expression.RECORD(Function.name(call.fn), ty, call.arguments);
end match;
end toRecordExpression;

Expand Down
2 changes: 1 addition & 1 deletion Compiler/NFFrontEnd/NFClass.mo
Expand Up @@ -163,7 +163,7 @@ uniontype Class
ClassTree tree;
algorithm
tree := ClassTree.fromRecordConstructor(inputs, locals, out);
cls := INSTANCED_CLASS(Type.UNKNOWN(), tree, Sections.EMPTY(), Restriction.FUNCTION());
cls := INSTANCED_CLASS(Type.UNKNOWN(), tree, Sections.EMPTY(), Restriction.RECORD_CONSTRUCTOR());
end makeRecordConstructor;

function initExpandedClass
Expand Down
4 changes: 4 additions & 0 deletions Compiler/NFFrontEnd/NFComplexType.mo
Expand Up @@ -53,6 +53,10 @@ public
Boolean isExpandable;
end CONNECTOR;

record RECORD
InstNode constructor;
end RECORD;

record EXTERNAL_OBJECT
InstNode constructor;
InstNode destructor;
Expand Down
8 changes: 7 additions & 1 deletion Compiler/NFFrontEnd/NFConvertDAE.mo
Expand Up @@ -38,6 +38,7 @@ import FlatModel = NFFlatModel;
import NFFlatten.FunctionTree;
import NFInstNode.InstNode;
import Statement = NFStatement;
import Restriction = NFRestriction;

protected
import ExecStat.execStat;
Expand Down Expand Up @@ -911,7 +912,7 @@ algorithm
cls := InstNode.getClass(Function.instance(func));

dfunc := match cls
case Class.INSTANCED_CLASS(sections = sections)
case Class.INSTANCED_CLASS(sections = sections, restriction = Restriction.FUNCTION())
algorithm
elems := convertFunctionParams(func.inputs, {});
elems := convertFunctionParams(func.outputs, elems);
Expand All @@ -935,6 +936,11 @@ algorithm
then
Function.toDAE(func, {def});

case Class.INSTANCED_CLASS(restriction = Restriction.RECORD_CONSTRUCTOR())
then DAE.Function.RECORD_CONSTRUCTOR(Function.name(func),
Function.makeDAEType(func),
DAE.emptyElementSource);

else
algorithm
Error.assertion(false, getInstanceName() + " got unknown function", sourceInfo());
Expand Down
43 changes: 31 additions & 12 deletions Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -852,30 +852,49 @@ function collectTypeFuncs
input output FunctionTree funcs;
algorithm
() := match ty
local
InstNode con, de;

// Collect external object structors.
case Type.COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT())
case Type.COMPLEX(complexTy = ComplexType.EXTERNAL_OBJECT(constructor = con, destructor = de))
algorithm
funcs := collectStructor(con, funcs);
funcs := collectStructor(de, funcs);
then
();

// Collect record constructors.
case Type.COMPLEX(complexTy = ComplexType.RECORD(constructor = con))
algorithm
funcs := collectExternalObjectStructors(ty.complexTy, funcs);
funcs := collectStructor(con, funcs);
then
();

else ();
end match;
end collectTypeFuncs;

function collectExternalObjectStructors
input ComplexType ty;
function collectStructor
input InstNode node;
input output FunctionTree funcs;
protected
InstNode constructor, destructor;
Function fn;
CachedData cache;
list<Function> fn;
algorithm
ComplexType.EXTERNAL_OBJECT(constructor, destructor) := ty;
CachedData.FUNCTION(funcs = {fn}) := InstNode.getFuncCache(constructor);
funcs := flattenFunction(fn, funcs);
CachedData.FUNCTION(funcs = {fn}) := InstNode.getFuncCache(destructor);
funcs := flattenFunction(fn, funcs);
end collectExternalObjectStructors;
cache := InstNode.getFuncCache(node);

() := match cache
case CachedData.FUNCTION()
algorithm
for fn in cache.funcs loop
funcs := flattenFunction(fn, funcs);
end for;
then
();

else ();
end match;
end collectStructor;

function collectEquationFuncs
input Equation eq;
Expand Down
26 changes: 20 additions & 6 deletions Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -293,17 +293,14 @@ uniontype Function
protected
CachedData cache;
algorithm
// Look up the the function.
// Look up the function.
fn_node := ComponentRef.node(fn_ref);
cache := InstNode.getFuncCache(fn_node);

// Check if a cached instantiation of this function already exists.
(fn_node, specialBuiltin) := match cache
case CachedData.FUNCTION() then (fn_node, cache.specialBuiltin);
else algorithm
(fn_node, specialBuiltin) := instFunc2(ComponentRef.toPath(fn_ref), fn_node, info);
//instFuncExpressions(fn_node);
then (fn_node, specialBuiltin);
else instFunc2(ComponentRef.toPath(fn_ref), fn_node, info);
end match;
end instFuncRef;

Expand Down Expand Up @@ -945,6 +942,13 @@ uniontype Function
end isTyped;

function typeFunction
input output Function fn;
algorithm
fn := typeFunctionSignature(fn);
fn := typeFunctionBody(fn);
end typeFunction;

function typeFunctionSignature
"Types a function's parameters, local components and default arguments."
input output Function fn;
protected
Expand All @@ -969,7 +973,7 @@ uniontype Function
checkParamTypes(fn);
fn.returnType := makeReturnType(fn);
end if;
end typeFunction;
end typeFunctionSignature;

function typeFunctionBody
"Types the body of a function, along with any bindings of local variables
Expand Down Expand Up @@ -1113,6 +1117,16 @@ uniontype Function
end match;
end inlineBuiltin;

function isDefaultRecordConstructor
input Function fn;
output Boolean isConstructor;
algorithm
isConstructor := match Class.restriction(InstNode.getClass(fn.node))
case Restriction.RECORD_CONSTRUCTOR() then true;
else false;
end match;
end isDefaultRecordConstructor;

function toDAE
input Function fn;
input list<DAE.FunctionDefinition> defs;
Expand Down
49 changes: 48 additions & 1 deletion Compiler/NFFrontEnd/NFInst.mo
Expand Up @@ -95,6 +95,7 @@ import FlatModel = NFFlatModel;
import BindingOrigin = NFBindingOrigin;
import ElementSource;
import SimplifyModel = NFSimplifyModel;
import Record = NFRecord;

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

Expand Down Expand Up @@ -1784,9 +1785,11 @@ algorithm
// Instantiate local equation/algorithm sections.
sections := instSections(node, scope, sections);

ty := Type.COMPLEX(node, ComplexType.CLASS());
ty := makeComplexType(cls.restriction, node);
inst_cls := Class.INSTANCED_CLASS(ty, cls.elements, sections, cls.restriction);
InstNode.updateClass(inst_cls, node);

instComplexType(ty);
then
();

Expand Down Expand Up @@ -1823,6 +1826,50 @@ algorithm
end match;
end instExpressions;

function makeComplexType
input Restriction restriction;
input InstNode node;
output Type ty;
protected
ComplexType cty;
algorithm
cty := match restriction
case Restriction.RECORD() then ComplexType.RECORD(node);
else ComplexType.CLASS();
end match;

ty := Type.COMPLEX(node, cty);
end makeComplexType;

function instComplexType
input Type ty;
algorithm
() := match ty
local
InstNode node;
CachedData cache;

case Type.COMPLEX(complexTy = ComplexType.RECORD(node))
algorithm
cache := InstNode.getFuncCache(node);

() := match cache
case CachedData.FUNCTION() then ();
else
algorithm
InstNode.cacheInitFunc(node);
Record.instConstructors(InstNode.scopePath(node), node, InstNode.info(node));
then
();

end match;
then
();

else ();
end match;
end instComplexType;

function instBuiltinAttribute
input output Modifier attribute;
algorithm
Expand Down

0 comments on commit 803cabd

Please sign in to comment.