diff --git a/Compiler/FrontEnd/DAEUtil.mo b/Compiler/FrontEnd/DAEUtil.mo index e6e7fc6ea7e..328e2250f27 100644 --- a/Compiler/FrontEnd/DAEUtil.mo +++ b/Compiler/FrontEnd/DAEUtil.mo @@ -1069,11 +1069,9 @@ algorithm DAE.Type tp; DAE.InstDims dim; DAE.ConnectorType ct; - DAE.VarVisibility prot; Option bind; Option dae_var_attr; Option comment; - Absyn.Path newtype; Absyn.InnerOuter io; DAE.ElementSource source "the element origin"; @@ -1081,7 +1079,6 @@ algorithm kind = kind, direction = dir, parallelism = prl, - protection = prot, ty = tp, binding = bind, dims = dim, @@ -1106,7 +1103,6 @@ algorithm local DAE.ComponentRef cr; DAE.VarKind kind; - DAE.VarDirection dir; DAE.VarParallelism prl; DAE.Type tp; DAE.InstDims dim; @@ -1115,13 +1111,11 @@ algorithm Option bind; Option dae_var_attr; Option comment; - Absyn.Path newtype; Absyn.InnerOuter io; DAE.ElementSource source "the element origin"; case (DAE.VAR(componentRef = cr, kind = kind, - direction = dir, parallelism = prl, protection = prot, ty = tp, @@ -1138,6 +1132,34 @@ algorithm end match; end setElementVarDirection; +public function setElementVarBinding + "Sets the binding of a VAR DAE.Element." + input DAE.Element inElement; + input Option inBinding; + output DAE.Element outElement; +algorithm + outElement := match(inElement, inBinding) + local + DAE.ComponentRef cr; + DAE.VarKind vk; + DAE.VarDirection vd; + DAE.VarParallelism vp; + DAE.VarVisibility vv; + DAE.Type ty; + DAE.InstDims dims; + DAE.ConnectorType ct; + DAE.ElementSource src; + Option va; + Option cmt; + Absyn.InnerOuter io; + + case (DAE.VAR(cr, vk, vd, vp, vv, ty, _, dims, ct, src, va, cmt, io),_) + then DAE.VAR(cr, vk, vd, vp, vv, ty, inBinding, dims, ct, src, va, cmt, io); + + else inElement; + end match; +end setElementVarBinding; + public function setProtectedAttr " sets the start attribute. If NONE(), assumes Real attributes." input Option attr; diff --git a/Compiler/FrontEnd/InstVar.mo b/Compiler/FrontEnd/InstVar.mo index 8d2ed3bab94..3725ba582e0 100644 --- a/Compiler/FrontEnd/InstVar.mo +++ b/Compiler/FrontEnd/InstVar.mo @@ -1041,7 +1041,7 @@ protected function instScalar2 algorithm outDae := match(inCref, inType, inVariability, inMod, inDae, inClassDae, inSource, inImpl) local - DAE.DAElist dae; + DAE.DAElist dae, cls_dae; // Constant with binding. case (_, _, SCode.CONST(), DAE.MOD(eqModOption = SOME(DAE.TYPED(modifierAsExp = _))), @@ -1096,15 +1096,69 @@ algorithm // All other scalars. else equation - dae = InstBinding.instModEquation(inCref, inType, inMod, inSource, inImpl); - dae = Util.if_(Types.isComplexType(inType), dae, DAE.emptyDae); + dae = Debug.bcallret5(Types.isComplexType(inType), + InstBinding.instModEquation, inCref, inType, inMod, inSource, inImpl, + DAE.emptyDae); + cls_dae = stripRecordDefaultBindingsFromDAE(inClassDae, inType, dae); dae = DAEUtil.joinDaes(dae, inDae); - dae = DAEUtil.joinDaes(inClassDae, dae); + dae = DAEUtil.joinDaes(cls_dae, dae); then dae; end match; end instScalar2; +protected function stripRecordDefaultBindingsFromDAE + "This function removes bindings from record members for which a binding + equation has already been generated. This is done because the record members + otherwise get a binding from the default argument of the record too." + input DAE.DAElist inClassDAE; + input DAE.Type inType; + input DAE.DAElist inEqDAE; + output DAE.DAElist outClassDAE; +algorithm + outClassDAE := match(inClassDAE, inType, inEqDAE) + local + list els, eqs; + + // Check if the component is of record type, and if any equations have been + // generated for the component's binding. + case (DAE.DAE(elementLst = els), + DAE.T_COMPLEX(complexClassType = ClassInf.RECORD(path = _)), + DAE.DAE(elementLst = eqs as _ :: _)) + equation + // This assumes that the equations are ordered the same as the variables. + (els, _) = List.mapFold(els, stripRecordDefaultBindingsFromElement, eqs); + then + DAE.DAE(els); + + else inClassDAE; + end match; +end stripRecordDefaultBindingsFromDAE; + +protected function stripRecordDefaultBindingsFromElement + input DAE.Element inVar; + input list inEqs; + output DAE.Element outVar; + output list outEqs; +algorithm + (outVar, outEqs) := matchcontinue(inVar, inEqs) + local + DAE.ComponentRef var_cr, eq_cr; + list rest_eqs; + + case (DAE.VAR(componentRef = var_cr), + DAE.EQUATION(exp = DAE.CREF(componentRef = eq_cr)) :: rest_eqs) + equation + true = ComponentReference.crefEqual(var_cr, eq_cr); + // The first equation assigns the variable. Remove the variable's + // binding and discard the equation. + then + (DAEUtil.setElementVarBinding(inVar, NONE()), rest_eqs); + + else (inVar, inEqs); + end matchcontinue; +end stripRecordDefaultBindingsFromElement; + protected function instArray "When an array is instantiated by instVar, this function is used to go through all the array elements and instantiate each array