Skip to content

Commit

Permalink
#3096 Implement prototype for dot operator
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24168 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jan 23, 2015
1 parent 5687737 commit e89830e
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 78 deletions.
10 changes: 7 additions & 3 deletions Compiler/BackEnd/EvaluateFunctions.mo
Expand Up @@ -294,6 +294,7 @@ algorithm
list<list<DAE.Exp>> scalarExp;
list<DAE.Statement> stmts;
list<DAE.Type> outputVarTypes;
list<String> outputVarNames;
list<list<DAE.ComponentRef>> scalarInputs, scalarOutputs;
case(DAE.CALL(path=path, expLst=exps, attr=attr1),_,_,_)
equation
Expand Down Expand Up @@ -423,7 +424,8 @@ algorithm
// which rhs
newOutputVars = List.filterOnTrue(updatedVarOutputs,DAEUtil.isOutputVar);
outputVarTypes = List.map(newOutputVars,DAEUtil.getVariableType);
attr2 = DAEUtil.replaceCallAttrType(attr1,DAE.T_TUPLE(outputVarTypes,DAE.emptyTypeSource));
outputVarNames = List.map(newOutputVars,DAEUtil.varName);
attr2 = DAEUtil.replaceCallAttrType(attr1,DAE.T_TUPLE(outputVarTypes,SOME(outputVarNames),DAE.emptyTypeSource));
DAE.CALL_ATTR(ty = singleOutputType) = attr1;
singleOutputType = if List.isNotEmpty(newOutputVars) then List.first(outputVarTypes) else singleOutputType;//if the function is evaluated completely
attr1 = DAEUtil.replaceCallAttrType(attr1,singleOutputType);
Expand Down Expand Up @@ -1058,11 +1060,13 @@ algorithm
DAE.Type outType;
list<DAE.Type> outTypeLst;
list<DAE.FuncArg> inputs;
list<String> outNames;
case(DAE.T_FUNCTION(funcArg = inputs, funcResultType = outType, functionAttributes = atts, source = source),_,_)
equation
//print("the out types1: "+Types.unparseType(outType)+"\n");
outTypeLst = List.map(outputs,DAEUtil.getVariableType);
outType = if intEq(listLength(outTypeLst),1) then List.first(outTypeLst) else DAE.T_TUPLE(outTypeLst,DAE.emptyTypeSource);
outTypeLst = list(DAEUtil.getVariableType(o) for o in outputs);
outNames = list(DAEUtil.varName(o) for o in outputs);
outType = if intEq(listLength(outTypeLst),1) then List.first(outTypeLst) else DAE.T_TUPLE(outTypeLst,SOME(outNames),DAE.emptyTypeSource);
outType = DAE.T_FUNCTION(inputs,outType,atts,source);
//print("the out types2: "+Types.unparseType(outType)+"\n");
then
Expand Down
33 changes: 25 additions & 8 deletions Compiler/FrontEnd/Absyn.mo
Expand Up @@ -795,6 +795,10 @@ uniontype Exp "The Exp uniontype is the container of a Modelica expression.
list<Exp> exps;
end LIST;

record DOT "exp.index"
Exp exp, index;
end DOT;

end Exp;

uniontype Case "case in match or matchcontinue"
Expand Down Expand Up @@ -1578,7 +1582,7 @@ algorithm
list<list<Exp>> mat_expl;
FunctionArgs fargs;
String error_msg;
Ident id;
Ident id, enterName, exitName;
MatchType match_ty;
list<ElementItem> match_decls;
list<Case> match_cases;
Expand Down Expand Up @@ -1708,10 +1712,19 @@ algorithm
case (CODE(), _, _, _)
then (inExp, inArg);

else
case (DOT(), _, _, arg)
equation
error_msg = "in traverseExpBidirSubExps - Unknown expression: ";
error_msg = error_msg + Dump.printExpStr(inExp);
(e1, arg) = traverseExpBidir(inExp.exp, enterFunc, exitFunc, arg);
(e2, arg) = traverseExpBidir(inExp.index, enterFunc, exitFunc, arg);
then
(DOT(e1, e2), arg);

else
algorithm
(,,enterName) := System.dladdr(enterFunc);
(,,exitName) := System.dladdr(exitFunc);
error_msg := "in traverseExpBidirSubExps(" + enterName + ", " + exitName + ") - Unknown expression: ";
error_msg := error_msg + Dump.printExpStr(inExp);
Error.addMessage(Error.INTERNAL_ERROR, {error_msg});
then
fail();
Expand Down Expand Up @@ -3371,7 +3384,7 @@ public function getCrefFromExp
input Boolean includeFunctions "note that if you say includeSubs = false then you won't get the functions from array subscripts";
output list<ComponentRef> outComponentRefLst;
algorithm
outComponentRefLst := matchcontinue (inExp,includeSubs,includeFunctions)
outComponentRefLst := match (inExp,includeSubs,includeFunctions)
local
ComponentRef cr;
list<ComponentRef> l1,l2,res;
Expand Down Expand Up @@ -3515,11 +3528,15 @@ algorithm

case (MATCHEXP(),_,_) then fail();

case (e1,_,_)
case (DOT(),_,_)
// inExp.index is only allowed to contain names to index the function call; not crefs that are evaluated in any way
then getCrefFromExp(inExp.exp,includeSubs,includeFunctions);

else
equation
print("Internal error: getCrefFromExp failed " + Dump.printExpStr(e1) + "\n");
Error.addInternalError(getInstanceName() + " failed " + Dump.printExpStr(inExp), sourceInfo());
then fail();
end matchcontinue;
end match;
end getCrefFromExp;

public function getCrefFromFarg "Returns the flattened list of all component references
Expand Down
3 changes: 2 additions & 1 deletion Compiler/FrontEnd/DAE.mo
Expand Up @@ -910,6 +910,7 @@ public uniontype Type "models the different front-end and back-end types"

record T_TUPLE
list<Type> types "For functions returning multiple values.";
Option<list<String>> names "For tuples elements that have names (function outputs)";
TypeSource source;
end T_TUPLE;

Expand Down Expand Up @@ -1107,7 +1108,7 @@ uniontype TupleConst "A tuple is added to the Types. This is used by functions w
end SINGLE_CONST;

record TUPLE_CONST
list<TupleConst> tupleConstLst "tupleConstLst" ;
list<TupleConst> tupleConstLst;
end TUPLE_CONST;

end TupleConst;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Expression.mo
Expand Up @@ -2224,7 +2224,7 @@ algorithm
equation
tys = List.map(exps, typeof);
then
DAE.T_TUPLE(tys, DAE.emptyTypeSource);
DAE.T_TUPLE(tys, NONE(), DAE.emptyTypeSource);
case (DAE.META_OPTION(_))then DAE.T_METATYPE(DAE.T_NONE_DEFAULT, DAE.emptyTypeSource);
case (DAE.METARECORDCALL(path=p, index = i))
equation
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/ExpressionSimplify.mo
Expand Up @@ -5536,7 +5536,7 @@ algorithm
then listGet(expl, i);
case DAE.TSUB(exp = DAE.TUPLE(PR = expl), ix = i)
then listGet(expl, i);
case DAE.TSUB(exp= e as (DAE.RCONST(_))) then e;
case DAE.TSUB(exp= e as (DAE.RCONST())) then e;
else origExp;
end match;
end simplifyTSub;
Expand Down
11 changes: 6 additions & 5 deletions Compiler/FrontEnd/InstSection.mo
Expand Up @@ -1252,10 +1252,11 @@ algorithm
DAE.TypeSource ts;
list<DAE.TupleConst> tupleConst,tupleConst2;
DAE.Const tconst;
Option<list<String>> names;

case(Absyn.TUPLE(aexpl),
DAE.PROP_TUPLE( DAE.T_TUPLE(typeList, _), _),
(DAE.PROP_TUPLE(DAE.T_TUPLE(lst,ts), DAE.TUPLE_CONST(tupleConst)
DAE.PROP_TUPLE( DAE.T_TUPLE(types=typeList,names=names), _),
(DAE.PROP_TUPLE(DAE.T_TUPLE(types=lst,source=ts), DAE.TUPLE_CONST(tupleConst)
)))
equation
fillValue = (listLength(typeList)-listLength(aexpl));
Expand All @@ -1266,9 +1267,9 @@ algorithm
lst = listAppend(lst,lst2);
tupleConst = listAppend(tupleConst,tupleConst2);
then
(Absyn.TUPLE(aexpl),DAE.PROP_TUPLE(DAE.T_TUPLE(lst,ts),DAE.TUPLE_CONST(tupleConst)));
(Absyn.TUPLE(aexpl),DAE.PROP_TUPLE(DAE.T_TUPLE(lst,names,ts),DAE.TUPLE_CONST(tupleConst)));

case(_, DAE.PROP_TUPLE(DAE.T_TUPLE(typeList,_), _), DAE.PROP(propType,tconst))
case(_, DAE.PROP_TUPLE(DAE.T_TUPLE(typeList,names,_), _), DAE.PROP(propType,tconst))
equation
fillValue = (listLength(typeList)-1);
aexpl2 = List.fill(Absyn.CREF(Absyn.WILD()),fillValue) "epxressions";
Expand All @@ -1278,7 +1279,7 @@ algorithm
lst = propType::lst2;
tupleConst = DAE.SINGLE_CONST(tconst)::tupleConst2;
then
(Absyn.TUPLE(aexpl),DAE.PROP_TUPLE(DAE.T_TUPLE(lst,DAE.emptyTypeSource),DAE.TUPLE_CONST(tupleConst)));
(Absyn.TUPLE(aexpl),DAE.PROP_TUPLE(DAE.T_TUPLE(lst,names,DAE.emptyTypeSource),DAE.TUPLE_CONST(tupleConst)));

case(_,_,_)
equation
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -1693,7 +1693,7 @@ algorithm

else
equation
print("Inst.getCrefFromMod: could not retrieve crefs from SCode.Mod: " + SCodeDump.printModStr(inMod,SCodeDump.defaultOptions) + "\n");
print(getInstanceName() + ": could not retrieve crefs from SCode.Mod: " + SCodeDump.printModStr(inMod,SCodeDump.defaultOptions) + "\n");
then
fail();

Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/NFTypeCheck.mo
Expand Up @@ -1609,7 +1609,7 @@ algorithm

case {ty} then (ty, false);

else (DAE.T_TUPLE(inTypeLst,DAE.emptyTypeSource), true);
else (DAE.T_TUPLE(inTypeLst,NONE(),DAE.emptyTypeSource), true);

end match;
end makeCallReturnType;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/OperatorOverloading.mo
Expand Up @@ -1438,7 +1438,7 @@ algorithm
case (_,_,{},_,_,_)
equation
(cache,Util.SUCCESS()) = Static.instantiateDaeFunctionFromTypes(inCache, env, acc, false, NONE(), true, Util.SUCCESS());
(DAE.T_TUPLE(funcs,{}),_) = Types.traverseType(DAE.T_TUPLE(acc,{}), -1, Types.makeExpDimensionsUnknown);
(DAE.T_TUPLE(funcs,_,{}),_) = Types.traverseType(DAE.T_TUPLE(acc,NONE(),{}), -1, Types.makeExpDimensionsUnknown);
then (cache,funcs);
end matchcontinue;
end getOperatorFuncsOrEmpty;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/FrontEnd/Patternm.mo
Expand Up @@ -2862,7 +2862,7 @@ algorithm
equation
tys2 = List.map(tys, Types.unboxedType);
(exps,tys2) = Types.matchTypeTuple(exps, tys, tys2, false);
then (DAE.TUPLE(exps),DAE.T_TUPLE(tys2,source));
then (DAE.TUPLE(exps),DAE.T_TUPLE(tys2,NONE(),source));
else (inExp,inType);
end match;
end makeTupleFromMetaTuple;
Expand Down
58 changes: 49 additions & 9 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -336,6 +336,7 @@ algorithm
case Absyn.CONS() then elabExp_Cons;
case Absyn.LIST() then elabExp_List;
case Absyn.MATCHEXP() then Patternm.elabMatchExpression;
case Absyn.DOT() then elabExp_Dot;
else elabExp_BuiltinType;
end match;

Expand Down Expand Up @@ -552,6 +553,48 @@ algorithm
end match;
end elabExp_Call;

protected function elabExp_Dot
extends PartialElabExpFunc;
algorithm
(outExp, outProperties) := match(inExp)
local
String s;
DAE.Type ty;
case Absyn.DOT()
algorithm
s := match inExp.index
case Absyn.CREF(Absyn.CREF_IDENT(name=s)) then s;
else
algorithm
Error.addSourceMessage(Error.COMPILER_ERROR, {"Dot operator is only allowed when indexing using a single simple name, got: " + Dump.printExpStr(inExp.index)}, inInfo);
then fail();
end match;
(outCache,outExp,outProperties,outST) := elabExp(inCache,inEnv,inExp.exp,inImplicit,inST, inDoVect, inPrefix, inInfo);
ty := Types.getPropType(outProperties);
_ := match ty
local
list<String> names;
Integer i;
case DAE.T_TUPLE(names=SOME(names))
algorithm
if not listMember(s, names) then
Error.addSourceMessage(Error.COMPILER_ERROR, {"Dot operator could not find " + s + " in " + Types.unparseType(ty)}, inInfo);
fail();
end if;
i := List.position(s, names);
outExp := DAE.TSUB(outExp, i, listGet(ty.types,i));
outProperties := DAE.PROP(listGet(ty.types,i), Types.propAllConst(outProperties));
then ();
else
algorithm
Error.addSourceMessage(Error.COMPILER_ERROR, {"Dot operator is only allowed when the expression returns a named tuple. Got expression: " + ExpressionDump.printExpStr(outExp) + " with type " + Types.unparseType(ty)}, inInfo);
then fail();
end match;
then (outExp, outProperties);

end match;
end elabExp_Dot;

protected function elabExp_PartEvalFunction
"turns an Absyn.PARTEVALFUNCTION into an DAE.PARTEVALFUNCTION"
extends PartialElabExpFunc;
Expand Down Expand Up @@ -1931,7 +1974,7 @@ algorithm
(cache,es_1,props) = elabTuple(cache,env,es,impl,false,pre,info);
(types,consts) = splitProps(props);
then
(cache,DAE.TUPLE(es_1),DAE.PROP_TUPLE(DAE.T_TUPLE(types,DAE.emptyTypeSource),DAE.TUPLE_CONST(consts)));
(cache,DAE.TUPLE(es_1),DAE.PROP_TUPLE(DAE.T_TUPLE(types,NONE(),DAE.emptyTypeSource),DAE.TUPLE_CONST(consts)));

// array-related expressions
case (cache,env,Absyn.RANGE(start = start,step = NONE(),stop = stop),impl,pre,_)
Expand Down Expand Up @@ -7200,8 +7243,6 @@ algorithm
args_2 = slotListArgs(newslots2);

tp = complexTypeFromSlots(newslots2,ClassInf.UNKNOWN(Absyn.IDENT("")));
//tyconst = elabConsts(outtype, const);
//prop = getProperties(outtype, tyconst);
then
(cache,SOME((DAE.CALL(fn,args_2,DAE.CALL_ATTR(tp,false,false,false,false,DAE.NO_INLINE(),DAE.NO_TAIL())),DAE.PROP(DAE.T_UNKNOWN_DEFAULT,DAE.C_CONST()))));

Expand Down Expand Up @@ -7790,7 +7831,7 @@ algorithm
str := "Failed to match types:\n actual: " +
Types.unparseType(Types.getPropType(prop)) +
"\n expected: " +
Types.unparseType(DAE.T_TUPLE(tys, DAE.emptyTypeSource));
Types.unparseType(DAE.T_TUPLE(tys, NONE(), DAE.emptyTypeSource));
fn_str := Absyn.pathString(fq_path);
Error.addSourceMessage(Error.META_RECORD_FOUND_FAILURE, {fn_str, str}, inInfo);
then
Expand Down Expand Up @@ -8997,11 +9038,10 @@ algorithm
then
DAE.T_ARRAY(ty, {dim}, ts);

case DAE.T_TUPLE(tys, ts)
case ty as DAE.T_TUPLE()
algorithm
tys := List.map2(tys, evaluateFuncArgTypeDims, inEnv, inCache);
then
DAE.T_TUPLE(tys, ts);
ty.types := List.map2(ty.types, evaluateFuncArgTypeDims, inEnv, inCache);
then ty;

else inType;

Expand Down Expand Up @@ -12882,7 +12922,7 @@ algorithm
prop := DAE.PROP(DAE.T_METATUPLE(tys2, DAE.emptyTypeSource), c);
else
exp := DAE.TUPLE(exps);
prop := DAE.PROP_TUPLE(DAE.T_TUPLE(types, DAE.emptyTypeSource), DAE.TUPLE_CONST(consts));
prop := DAE.PROP_TUPLE(DAE.T_TUPLE(types, NONE(), DAE.emptyTypeSource), DAE.TUPLE_CONST(consts));
end if;
end fixTupleMetaModelica;

Expand Down

0 comments on commit e89830e

Please sign in to comment.