diff --git a/Compiler/FrontEnd/ExpressionSimplify.mo b/Compiler/FrontEnd/ExpressionSimplify.mo index b3ec292dff..57a2bbf3d1 100644 --- a/Compiler/FrontEnd/ExpressionSimplify.mo +++ b/Compiler/FrontEnd/ExpressionSimplify.mo @@ -282,6 +282,8 @@ algorithm case (DAE.CALL(),_) then (simplifyCall(inExp),options); + case (DAE.RSUB(),_) then (simplifyRSub(inExp),options); + case (DAE.MATCHEXPRESSION(),_) then (simplifyMatch(inExp),options); case (DAE.UNBOX(),_) then (simplifyUnbox(inExp),options); case (DAE.BOX(),_) then (simplifyUnbox(inExp),options); @@ -302,6 +304,18 @@ algorithm end match; end simplifyWork; +protected function simplifyRSub + input output DAE.Exp e; +algorithm + e := match e + local + DAE.ComponentRef cr; + case (DAE.RSUB(exp=DAE.CREF(componentRef=cr), ix=-1)) + then DAE.CREF(ComponentReference.joinCrefs(cr, ComponentReference.makeCrefIdent(e.fieldName, e.ty, {})), e.ty); + else e; + end match; +end simplifyRSub; + protected function simplifyAsubExp input DAE.Exp origExp; input DAE.Exp inExp; diff --git a/Compiler/FrontEnd/Inline.mo b/Compiler/FrontEnd/Inline.mo index 05bda01c97..1bdb37e26c 100644 --- a/Compiler/FrontEnd/Inline.mo +++ b/Compiler/FrontEnd/Inline.mo @@ -1424,7 +1424,7 @@ public function replaceArgs algorithm (outExp,outTuple) := matchcontinue (inExp,inTuple) local - DAE.ComponentRef cref; + DAE.ComponentRef cref, firstCref; list> argmap; DAE.Exp e; Absyn.Path path; @@ -1447,6 +1447,18 @@ algorithm BaseHashTable.hasKey(ComponentReference.crefFirstCref(cref),checkcr) then (inExp,(argmap,checkcr,false)); + case (DAE.CREF(componentRef = cref),(argmap,checkcr,true)) + algorithm + firstCref := ComponentReference.crefFirstCref(cref); + {} := ComponentReference.crefSubs(firstCref); + e := getExpFromArgMap(argmap,firstCref); + while not ComponentReference.crefIsIdent(cref) loop + cref := ComponentReference.crefRest(cref); + {} := ComponentReference.crefSubs(cref); + e := DAE.RSUB(e, -1, ComponentReference.crefFirstIdent(cref), ComponentReference.crefType(cref)); + end while; + then (e,inTuple); + case (DAE.CREF(componentRef = cref),(argmap,checkcr,true)) equation getExpFromArgMap(argmap,ComponentReference.crefStripSubs(ComponentReference.crefFirstCref(cref))); diff --git a/Compiler/Template/CodegenCFunctions.tpl b/Compiler/Template/CodegenCFunctions.tpl index db313ebdfa..b36b18f44b 100644 --- a/Compiler/Template/CodegenCFunctions.tpl +++ b/Compiler/Template/CodegenCFunctions.tpl @@ -3699,6 +3699,7 @@ template expTypeFromExpFlag(Exp exp, Integer flag) case e as CONS(__) case e as LIST(__) case e as SIZE(__) then expTypeFlag(typeof(e), flag) + case c as RSUB(ix=-1) then expTypeFlag(c.ty, flag) case META_TUPLE(__) case META_OPTION(__) @@ -6246,6 +6247,9 @@ template daeExpRsub(Exp inExp, Context context, Text &preExp, "Generates code for an tsub expression." ::= match inExp + case RSUB(ix=-1) then + let res = daeExp(exp, context, &preExp, &varDecls, &auxFunction) + '<%res%>._<%fieldName%>' case RSUB(__) then let res = daeExp(exp, context, &preExp, &varDecls, &auxFunction) let offset = intAdd(ix,1) // 1-based diff --git a/Compiler/Template/CodegenCppCommon.tpl b/Compiler/Template/CodegenCppCommon.tpl index b7e83e63b1..f7e21c78c9 100644 --- a/Compiler/Template/CodegenCppCommon.tpl +++ b/Compiler/Template/CodegenCppCommon.tpl @@ -973,10 +973,22 @@ template daeExp(Exp exp, Context context, Text &preExp /*BUFP*/, Text &varDecls case e as PARTEVALFUNCTION(__)then daeExpPartEvalFunction(e, context, &preExp, &varDecls, simCode , &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) case e as BOX(__) then daeExpBox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) case e as UNBOX(__) then daeExpUnbox(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) + case e as RSUB(__) then daeExpRSub(e, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) else error(sourceInfo(), 'Unknown exp:<%ExpressionDumpTpl.dumpExp(exp,"\"")%>') end daeExp; +template daeExpRSub(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, + Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation) + "Generates code for an tsub expression." +::= + match exp + case RSUB(ix=-1) then + let res = daeExp(exp, context, &preExp, &varDecls, simCode, &extraFuncs, &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation) + '<%res%>.<%fieldName%>_' + case RSUB(__) then + error(sourceInfo(), '<%ExpressionDumpTpl.dumpExp(exp,"\"")%>: failed') +end daeExpRSub; template daeExpRange(Exp exp, Context context, Text &preExp, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl, Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)