Skip to content

Commit

Permalink
Fix for #2662:
Browse files Browse the repository at this point in the history
- Remove equations and algorithms from base classes too when instantiating a
  function type.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@20133 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Apr 14, 2014
1 parent c8275d3 commit 80df834
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
68 changes: 47 additions & 21 deletions Compiler/FrontEnd/Inst.mo
Expand Up @@ -1035,7 +1035,7 @@ algorithm
outputs)),
/*SOME(FUNC_partialInstClassIn( // result for partial instantiation
(inCache,inEnv,inIH,inMod,inPrefix,inSets,inState,inClass,inVisibility,inInstDims),
(env,ci_state)))*/ NONE());
(env,ci_state)))*/ NONE(), callscope);
/*
Debug.fprintln(Flags.CACHE, "IIII->added to instCache: " +& Absyn.pathString(fullEnvPathPlusClass) +&
"\n\tpre: " +& PrefixUtil.printPrefixStr(pre) +& " class: " +& className +&
Expand Down Expand Up @@ -1874,7 +1874,7 @@ algorithm
addToInstCache(fullEnvPathPlusClass,
NONE(),
SOME(FUNC_partialInstClassIn( // result for partial instantiation
inputs,outputs)));
inputs,outputs)), InstTypes.INNER_CALL());
//Debug.fprintln(Flags.CACHE, "IIIIPARTIAL->added to instCache: " +& Absyn.pathString(fullEnvPathPlusClass));
then
(cache,env,ih,ci_state,vars);
Expand Down Expand Up @@ -2389,13 +2389,11 @@ algorithm
// Add components from base classes to be instantiated in 3 as well.
compelts_1 = List.flatten({extcomps,compelts_1,cdefelts_1});

// Take the union of the equations in the current scope and equations
// from extends, to filter out identical equations.
eqs_1 = List.unionOnTrue(eqs, eqs2, SCode.equationEqual);
initeqs_1 = List.unionOnTrue(initeqs, initeqs2, SCode.equationEqual);

alg_1 = listAppend(alg, alg2);
initalg_1 = listAppend(initalg, initalg2);
// Add equation and algorithm sections from base classes.
eqs_1 = joinExtEquations(eqs, eqs2, callscope);
initeqs_1 = joinExtEquations(initeqs, initeqs2, callscope);
alg_1 = joinExtAlgorithms(alg, alg2, callscope);
initalg_1 = joinExtAlgorithms(initalg, initalg2, callscope);

(compelts_1, eqs_1, initeqs_1, alg_1, initalg_1) =
InstUtil.extractConstantPlusDepsTpl(compelts_1, instSingleCref, {}, className, eqs_1, initeqs_1, alg_1, initalg_1);
Expand Down Expand Up @@ -2849,6 +2847,32 @@ algorithm
end matchcontinue;
end instClassdef2;

protected function joinExtEquations
input list<SCode.Equation> inEq;
input list<SCode.Equation> inExtEq;
input InstTypes.CallingScope inCallingScope;
output list<SCode.Equation> outEq;
algorithm
outEq := match(inEq, inExtEq, inCallingScope)
case (_, _, InstTypes.TYPE_CALL()) then {};
// Take the union of the equations in the current scope and equations
// from extends, to filter out identical equations.
else List.unionOnTrue(inEq, inExtEq, SCode.equationEqual);
end match;
end joinExtEquations;

protected function joinExtAlgorithms
input list<SCode.AlgorithmSection> inAlg;
input list<SCode.AlgorithmSection> inExtAlg;
input InstTypes.CallingScope inCallingScope;
output list<SCode.AlgorithmSection> outAlg;
algorithm
outAlg := match(inAlg, inExtAlg, inCallingScope)
case (_, _, InstTypes.TYPE_CALL()) then {};
else listAppend(inAlg, inExtAlg);
end match;
end joinExtAlgorithms;

protected function instClassDefHelper
"Function: instClassDefHelper
MetaModelica extension. KS TODO: Document this function!!!!"
Expand Down Expand Up @@ -5811,25 +5835,30 @@ end makeFullyQualified2;
// hash table implementation for cashing instantiation results
// *********************************************************************

function addToInstCache
protected function addToInstCache
input Absyn.Path fullEnvPathPlusClass;
input Option<CachedInstItem> fullInstOpt;
input Option<CachedInstItem> partialInstOpt;
input InstTypes.CallingScope inCallScope;
algorithm
_ := matchcontinue(fullEnvPathPlusClass,fullInstOpt, partialInstOpt)
_ := matchcontinue(fullEnvPathPlusClass,fullInstOpt, partialInstOpt, inCallScope)
local
CachedInstItem fullInst, partialInst;
InstHashTable instHash;

// Don'ẗ add classes that were only instantiated to get their type,
// they are not complete.
case (_, _, _, InstTypes.TYPE_CALL()) then ();

// nothing is we have +d=noCache
case (_, _, _)
case (_, _, _, _)
equation
false = Flags.isSet(Flags.CACHE);
then
();

// we have them both
case (_, SOME(_), SOME(_))
case (_, SOME(_), SOME(_), _)
equation
instHash = getGlobalRoot(Global.instHashIndex);
instHash = BaseHashTable.add((fullEnvPathPlusClass,{fullInstOpt,partialInstOpt}),instHash);
Expand All @@ -5838,7 +5867,7 @@ algorithm
();

// we have a partial inst result and the full in the cache
case (_, NONE(), SOME(_))
case (_, NONE(), SOME(_), _)
equation
instHash = getGlobalRoot(Global.instHashIndex);
// see if we have a full inst here
Expand All @@ -5849,7 +5878,7 @@ algorithm
();

// we have a partial inst result and the full is NOT in the cache
case (_, NONE(), SOME(_))
case (_, NONE(), SOME(_), _)
equation
instHash = getGlobalRoot(Global.instHashIndex);
// see if we have a full inst here
Expand All @@ -5860,7 +5889,7 @@ algorithm
();

// we have a full inst result and the partial in the cache
case (_, SOME(_), NONE())
case (_, SOME(_), NONE(), _)
equation
instHash = getGlobalRoot(Global.instHashIndex);
// see if we have a partial inst here
Expand All @@ -5871,7 +5900,7 @@ algorithm
();

// we have a full inst result and the partial is NOT in the cache
case (_, SOME(_), NONE())
case (_, SOME(_), NONE(), _)
equation
instHash = getGlobalRoot(Global.instHashIndex);
// see if we have a partial inst here
Expand All @@ -5882,10 +5911,7 @@ algorithm
();

// we failed above??!!
case (_, _, _)
equation
then
();
else ();
end matchcontinue;
end addToInstCache;

Expand Down
10 changes: 6 additions & 4 deletions Compiler/FrontEnd/InstFunction.mo
Expand Up @@ -399,6 +399,7 @@ algorithm
Boolean partialPrefixBool, isImpure;
SCode.Comment cmt;
SCode.FunctionRestriction funcRest;
InstTypes.CallingScope cs;

// normal functions
case (cache,env,ih,mod,pre,SCode.CLASS(classDef=cd,partialPrefix = partialPrefix, name = n,restriction = SCode.R_FUNCTION(funcRest),info = info,cmt=cmt),inst_dims,_)
Expand All @@ -410,9 +411,10 @@ algorithm
c = Util.if_(Config.acceptMetaModelicaGrammar(),
inClass,
SCode.setClassPartialPrefix(SCode.NOT_PARTIAL(), inClass));
cs = Util.if_(instFunctionTypeOnly, InstTypes.TYPE_CALL(), InstTypes.INNER_CALL());
(cache,cenv,ih,_,DAE.DAE(daeElts),_,ty,_,_,_) =
Inst.instClass(cache, env, ih, UnitAbsynBuilder.emptyInstStore(), mod, pre,
c, inst_dims, true, InstTypes.INNER_CALL(), ConnectionGraph.EMPTY, Connect.emptySet);
c, inst_dims, true, cs, ConnectionGraph.EMPTY, Connect.emptySet);
List.map2_0(daeElts,InstUtil.checkFunctionElement,false,info);
env_1 = Env.extendFrameC(env,c);
(cache,fpath) = Inst.makeFullyQualified(cache, env_1, Absyn.IDENT(n));
Expand Down Expand Up @@ -623,7 +625,7 @@ algorithm
encapsulatedPrefix = e,partialPrefix = p,restriction = _,
classDef = SCode.PARTS(elementLst = elts,externalDecl=_),cmt=cmt, info = info))
equation
elts = List.select(elts,isElementImportForFunctions);
elts = List.select(elts,isElementImportantForFunction);
stripped_class = SCode.CLASS(id,prefixes,e,p,SCode.R_FUNCTION(SCode.FR_NORMAL_FUNCTION(false)),SCode.PARTS(elts,{},{},{},{},{},{},NONE()),cmt,info);
(cache,env_1,ih,funs) = implicitFunctionInstantiation2(cache, env, ih, DAE.NOMOD(), Prefix.NOPRE(), stripped_class, {}, true);
// Only external functions are valid without an algorithm section...
Expand Down Expand Up @@ -999,7 +1001,7 @@ algorithm
end matchcontinue;
end addRecordConstructorFunction;

protected function isElementImportForFunctions
protected function isElementImportantForFunction
input SCode.Element elt;
output Boolean b;
algorithm
Expand All @@ -1009,6 +1011,6 @@ algorithm
then false;
else true;
end match;
end isElementImportForFunctions;
end isElementImportantForFunction;

end InstFunction;
1 change: 1 addition & 0 deletions Compiler/FrontEnd/InstTypes.mo
Expand Up @@ -44,6 +44,7 @@ uniontype CallingScope "
Calling scope is used to determine when unconnected flow variables should be set to zero."
record TOP_CALL "this is a top call" end TOP_CALL;
record INNER_CALL "this is an inner call" end INNER_CALL;
record TYPE_CALL "a call to determine type of a class" end TYPE_CALL;
end CallingScope;

type PolymorphicBindings = list<tuple<String,list<DAE.Type>>>;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -6440,7 +6440,7 @@ algorithm
outBoolean:=
match (inCallingScope)
case InstTypes.TOP_CALL() then true;
case InstTypes.INNER_CALL() then false;
else false;
end match;
end isTopCall;

Expand Down

0 comments on commit 80df834

Please sign in to comment.