Skip to content

Commit

Permalink
- Implemented function overloading
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10009 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 5, 2011
1 parent 4e36173 commit 511f0b7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
58 changes: 27 additions & 31 deletions Compiler/FrontEnd/Inst.mo
Expand Up @@ -10778,11 +10778,13 @@ algorithm

/* Instantiate overloaded functions */
case (cache,env,ih,mod,pre,(c as SCode.CLASS(name = n,restriction = (restr as SCode.R_FUNCTION()),
classDef = SCode.OVERLOAD(pathLst = funcnames))),inst_dims,_)
classDef = SCode.OVERLOAD(pathLst = funcnames,comment=cmt))),inst_dims,_)
equation
(cache,env_1,ih,resfns) = instOverloadedFunctions(cache,env,ih, n, funcnames) "Overloaded functions" ;
(cache,ih,resfns) = instOverloadedFunctions(cache,env,ih,pre,funcnames) "Overloaded functions" ;
(cache,fpath) = makeFullyQualified(cache,env,Absyn.IDENT(n));
resfns = DAE.FUNCTION(fpath,{DAE.FUNCTION_DEF({})},DAE.T_NOTYPE_DEFAULT,true,DAE.NO_INLINE(),DAE.emptyElementSource,cmt)::resfns;
then
(cache,env_1,ih,resfns);
(cache,env,ih,resfns);

// handle failure
case (_,env,_,_,_,SCode.CLASS(name=n),_,_)
Expand Down Expand Up @@ -11437,9 +11439,14 @@ algorithm
then
(cache,env_1,ih);

case (_,_,_,_)
case (cache,env,ih,SCode.CLASS(name = id,partialPrefix = p,encapsulatedPrefix = e,restriction = r,
classDef = SCode.OVERLOAD(pathLst=_),info = info))
then
(cache,env,ih);

case (_,_,_,SCode.CLASS(name=id))
equation
Debug.fprintln("failtrace", "- Inst.implicitFunctionTypeInstantiation failed");
Debug.fprintln("failtrace", "- Inst.implicitFunctionTypeInstantiation failed " +& id);
then fail();
end matchcontinue;
end implicitFunctionTypeInstantiation;
Expand All @@ -11452,14 +11459,13 @@ protected function instOverloadedFunctions
input Env.Cache inCache;
input Env.Env inEnv;
input InstanceHierarchy inIH;
input Absyn.Ident inIdent;
input Prefix.Prefix pre;
input list<Absyn.Path> inAbsynPathLst;
output Env.Cache outCache;
output Env.Env outEnv;
output InstanceHierarchy outIH;
output list<DAE.Function> outFns;
algorithm
(outCache,outEnv,outIH,outFns) := matchcontinue (inCache,inEnv,inIH,inIdent,inAbsynPathLst)
(outCache,outIH,outFns) := matchcontinue (inCache,inEnv,inIH,pre,inAbsynPathLst)
local
list<Env.Frame> env,cenv,env_1,env_2;
SCode.Element c;
Expand All @@ -11477,37 +11483,27 @@ algorithm
DAE.FunctionAttributes functionAttributes;
DAE.ElementSource source "the origin of the element";
Absyn.Info info;
list<DAE.Function> resfns;
list<DAE.Function> resfns,resfns1,resfns2;
DAE.Function resfn;
Boolean partialPrefixBool;
SCode.Restriction rest;

case (cache,env,ih,_,{}) then (cache,env,ih,{});
case (cache,env,ih,pre,{}) then (cache,ih,{});

// Instantiate each function, add its FQ name to the type, needed when deoverloading
case (cache,env,ih,overloadname,(fn :: fns))
equation
(cache,(c as SCode.CLASS(name=id,partialPrefix=partialPrefix,encapsulatedPrefix=encflag,restriction=SCode.R_FUNCTION(),info=info)),cenv) = Lookup.lookupClass(cache, env, fn, true);
(cache,_,ih,_,DAE.DAE(daeElts),_,(DAE.T_FUNCTION(args,tp,functionAttributes),_),st,_,_) =
instClass(cache,cenv,ih,UnitAbsynBuilder.emptyInstStore(),
DAE.NOMOD(), Prefix.NOPRE(), c, {}, true, INNER_CALL(), ConnectionGraph.EMPTY, Connect.emptySet);
(cache,fpath) = makeFullyQualified(cache,env, Absyn.IDENT(overloadname));
(cache,ovlfpath) = makeFullyQualified(cache,cenv, Absyn.IDENT(id));
ty = (DAE.T_FUNCTION(args,tp,functionAttributes),SOME(ovlfpath));
env_1 = Env.extendFrameT(env, overloadname, ty);
(cache,env_2,ih,resfns) = instOverloadedFunctions(cache,env_1,ih, overloadname, fns);
// TODO: Fix inline here
print(" DAE.InlineType FIX HERE \n");
// set the of this element
source = DAEUtil.createElementSource(info, Env.getEnvPath(env), NONE(), NONE(), NONE());
partialPrefixBool = SCode.partialBool(partialPrefix);
resfn = DAE.FUNCTION(fpath,{DAE.FUNCTION_DEF(daeElts)},ty,partialPrefixBool,DAE.NO_INLINE(),source,NONE());
then
(cache,env_2,ih,resfn::resfns);
case (cache,env,ih,pre,(fn :: fns))
equation
// print("instOvl: " +& Absyn.pathString(fn) +& "\n");
(cache,(c as SCode.CLASS(name=id,partialPrefix=partialPrefix,encapsulatedPrefix=encflag,restriction=rest,info=info)),cenv) = Lookup.lookupClass(cache, env, fn, true);
true = SCode.isFunctionOrExtFunction(rest);
(cache,_,ih,resfns1) = implicitFunctionInstantiation2(inCache, cenv, inIH, DAE.NOMOD(), pre, c, {}, false);
(cache,ih,resfns2) = instOverloadedFunctions(cache,env,ih,pre,fns);
then (cache,ih,listAppend(resfns1,resfns2));

// failure
case (_,env,ih,_,_)
case (cache,env,ih,pre,(fn :: fns))
equation
Debug.fprint("failtrace", "- Inst.instOverloaded_functions failed\n");
Debug.fprint("failtrace", "- Inst.instOverloaded_functions failed " +& Absyn.pathString(fn) +& "\n");
then
fail();
end matchcontinue;
Expand Down
38 changes: 38 additions & 0 deletions Compiler/FrontEnd/Lookup.mo
Expand Up @@ -1555,8 +1555,10 @@ public function lookupFunctionsInEnv
algorithm
(outCache,outTypesTypeLst) := matchcontinue (cache,env,id,info)
local
Env.Env env_1;
Env.Frame f;
list<DAE.Type> res;
list<Absyn.Path> names;
Env.AvlTree httypes;
Env.AvlTree ht;
String str;
Expand Down Expand Up @@ -1591,6 +1593,13 @@ algorithm
(cache,res) = lookupFunctionsInEnv2(cache,{f},id,true,info);
then (cache,res);

case (cache,env,id,info)
equation
(cache,SCode.CLASS(classDef=SCode.OVERLOAD(pathLst=names),info=info),env_1) = lookupClass(cache,env,id,false);
(cache,res) = lookupFunctionsListInEnv(cache,env_1,names,info,{});
// print(stringDelimitList(List.map(res,Types.unparseType),"\n###\n"));
then (cache,res);

case (cache,_,_,_) then (cache,{});
case (_,_,id,_)
equation
Expand All @@ -1601,6 +1610,35 @@ algorithm
end matchcontinue;
end lookupFunctionsInEnv;

protected function lookupFunctionsListInEnv
input Env.Cache cache;
input Env.Env env;
input list<Absyn.Path> ids;
input Absyn.Info info;
input list<DAE.Type> acc;
output Env.Cache outCache;
output list<DAE.Type> outTypesTypeLst;
algorithm
(outCache,outTypesTypeLst) := matchcontinue (cache,env,ids,info,acc)
local
Absyn.Path id;
list<DAE.Type> res;
String str;
case (cache,_,{},_,acc) then (cache,listReverse(acc));
case (cache,env,id::ids,info,acc)
equation
(cache,res as _::_) = lookupFunctionsInEnv(cache,env,id,info);

(cache,acc) = lookupFunctionsListInEnv(cache,env,ids,info,listAppend(res,acc));
then (cache,acc);
case (_,env,id::_,info,_)
equation
str = Absyn.pathString(id) +& " not found in scope: " +& Env.printEnvPathStr(env);
Error.addSourceMessage(Error.INTERNAL_ERROR, {str}, info);
then fail();
end matchcontinue;
end lookupFunctionsListInEnv;

protected function lookupFunctionsInEnv2
"function: lookupFunctionsInEnv
Returns a list of types that the function has."
Expand Down
6 changes: 5 additions & 1 deletion Compiler/FrontEnd/SCodeDependency.mo
Expand Up @@ -418,6 +418,7 @@ algorithm
Env ty_env, env;
Item ty_item;
SCode.Attributes attr;
list<Absyn.Path> paths;

// A class made of parts, analyse elements, equation, algorithms, etc.
case (SCode.PARTS(elementLst = el, normalEquationLst = nel,
Expand Down Expand Up @@ -471,7 +472,10 @@ algorithm

// Other cases which doesn't need to be analysed.
case (SCode.ENUMERATION(enumLst = _), _, _, _, _) then ();
case (SCode.OVERLOAD(pathLst = _), _, _, _, _) then ();
case (SCode.OVERLOAD(pathLst = paths), _, _, _, _)
equation
List.map2_0(paths,analyseClass,inEnv,inInfo);
then ();
case (SCode.PDER(functionPath = _), _, _, _, _) then ();

end matchcontinue;
Expand Down
3 changes: 3 additions & 0 deletions Compiler/susan_codegen/SimCode/SCodeDumpTpl.tpl
Expand Up @@ -210,6 +210,9 @@ match classDef
let func_str = dumpPath(functionPath)
let cmt_str = dumpCommentOpt(comment)
'= der(<%func_str%>, <%derivedVariables ;separator=", "%>)<%cmt_str%>'
case OVERLOAD(__) then
let cmt_str = dumpCommentOpt(comment)
'= overload(<%pathLst |> path => dumpPath(path); separator=", "%>)<%cmt_str%>'
else errorMsg("SCodeDump.dumpClassDef: Unknown class definition.")
end dumpClassDef;

Expand Down

0 comments on commit 511f0b7

Please sign in to comment.