Skip to content

Commit

Permalink
+ Generate default value bindings for protected components of a record.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@14958 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
mahge committed Jan 27, 2013
1 parent a14fcdb commit cae0afd
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
1 change: 1 addition & 0 deletions Compiler/BackEnd/SimCode.mo
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ uniontype Function
record RECORD_CONSTRUCTOR
Absyn.Path name;
list<Variable> funArgs;
list<Variable> locals;
Absyn.Info info;
end RECORD_CONSTRUCTOR;
end Function;
Expand Down
35 changes: 32 additions & 3 deletions Compiler/BackEnd/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ algorithm
Boolean dynamicLoad;
list<String> includeDirs;
DAE.FunctionAttributes funAttrs;
list<DAE.Var> varlst;

// Modelica functions.
case (_, DAE.FUNCTION(path = fpath, source = source,
Expand Down Expand Up @@ -819,9 +820,12 @@ algorithm
equation
funArgs = List.map(args, typesSimFunctionArg);
(recordDecls, rt_1) = elaborateRecordDeclarationsForRecord(restype, recordDecls, rt);
DAE.T_COMPLEX(varLst = varlst) = restype;
varlst = List.filterOnTrue(varlst, Types.isProtectedVar);
varDecls = List.map(varlst, typesVar);
info = DAEUtil.getElementSourceFileInfo(source);
then
(SimCode.RECORD_CONSTRUCTOR(name, funArgs, info), rt_1, recordDecls, includes, includeDirs, libs);
(SimCode.RECORD_CONSTRUCTOR(name, funArgs, varDecls, info), rt_1, recordDecls, includes, includeDirs, libs);

// failure
case (_, fn, _, _, _, _, _)
Expand Down Expand Up @@ -5446,7 +5450,7 @@ algorithm
equation
sname = Absyn.pathStringReplaceDot(name, "_");
false = listMember(sname, rt);
vars = List.map(varlst, typesVar);
vars = List.map(varlst, typesVarNoBinding);
rt_1 = sname :: rt;
(accRecDecls, rt_2) = elaborateNestedRecordDeclarations(varlst, accRecDecls, rt_1);
recDecl = SimCode.RECORD_DECL_FULL(sname, path, vars);
Expand Down Expand Up @@ -9399,7 +9403,7 @@ algorithm
end match;
end scodeParallelismToDAEParallelism;

protected function typesVar
protected function typesVarNoBinding
input Types.Var inTypesVar;
output SimCode.Variable outVar;
algorithm
Expand All @@ -9420,6 +9424,31 @@ algorithm
prl = scodeParallelismToDAEParallelism(scPrl);
then SimCode.VARIABLE(cref_, ty, NONE(), {}, prl);
end match;
end typesVarNoBinding;

protected function typesVar
input Types.Var inTypesVar;
output SimCode.Variable outVar;
algorithm
outVar := match (inTypesVar)
local
String name;
Types.Type ty;
DAE.ComponentRef cref_;
DAE.Attributes attr;
SCode.Parallelism scPrl;
DAE.VarParallelism prl;
DAE.Exp bindExp;

case (DAE.TYPES_VAR(name=name, attributes = attr, ty=ty))
equation
ty = Types.simplifyType(ty);
cref_ = ComponentReference.makeCrefIdent(name, ty, {});
DAE.ATTR(parallelism = scPrl) = attr;
prl = scodeParallelismToDAEParallelism(scPrl);
bindExp = Types.getBindingExp(inTypesVar, Absyn.IDENT(name));
then SimCode.VARIABLE(cref_, ty, SOME(bindExp), {}, prl);
end match;
end typesVar;

public function dlowvarToSimvar
Expand Down
15 changes: 14 additions & 1 deletion Compiler/FrontEnd/Types.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3049,7 +3049,7 @@ algorithm
end isPublicAttr;

public function isPublicVar
"Succeds if variable is a public variable."
"true if variable is a public variable."
input Var inVar;
output Boolean b;
algorithm
Expand All @@ -3061,6 +3061,19 @@ algorithm
end match;
end isPublicVar;

public function isProtectedVar
"true if variable is a protected variable."
input Var inVar;
output Boolean b;
algorithm
b := match (inVar)
local
Attributes attr;

case DAE.TYPES_VAR(attributes = attr) then not isPublicAttr(attr);
end match;
end isProtectedVar;


public function isModifiableTypesVar
input DAE.Var inVar;
Expand Down
48 changes: 48 additions & 0 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -4289,16 +4289,22 @@ match fn
case RECORD_CONSTRUCTOR(__) then
let()= System.tmpTickReset(1)
let &varDecls = buffer "" /*BUFD*/
let &varInits = buffer ""
let &varFrees = buffer ""
let fname = underscorePath(name)
let retType = '<%fname%>_rettype'
let retVar = tempDecl(retType, &varDecls /*BUFD*/)
let structType = 'struct <%fname%>'
let structVar = tempDecl(structType, &varDecls /*BUFD*/)
let _ = (locals |> var hasindex i1 fromindex 1 =>
varInitRecord(var, i1, structVar, &varDecls /*BUFD*/, &varInits /*BUFC*/) ; empty /* increase the counter! */
)
let boxedFn = if acceptMetaModelicaGrammar() then functionBodyBoxed(fn)
<<
<%retType%> omc_<%fname%>(<%funArgs |> VARIABLE(__) => '<%expTypeArrayIf(ty)%> <%crefStr(name)%>' ;separator=", "%>)
{
<%varDecls%>
<%varInits%>
<%funArgs |> VARIABLE(__) => '<%structVar%>.<%crefStr(name)%> = <%crefStr(name)%>;' ;separator="\n"%>
<%retVar%>.c1 = <%structVar%>;
return <%retVar%>;
Expand All @@ -4312,6 +4318,48 @@ case RECORD_CONSTRUCTOR(__) then
>>
end functionBodyRecordConstructor;

template varInitRecord(Variable var, Integer i, String prefix, Text &varDecls /*BUFP*/, Text &varInits /*BUFP*/)
"Generates code to initialize variables.
Does not return anything: just appends declarations to buffers."
::=
match var
case var as VARIABLE(parallelism = NON_PARALLEL(__)) then
let varName = '<%prefix%>.<%crefToCStr(var.name)%>'
let &varInits += initRecordMembers(var)
let instDimsInit = (instDims |> exp =>
daeExp(exp, contextFunction, &varInits /*BUFC*/, &varDecls /*BUFD*/)
;separator=", ")
if instDims then
(match var.value
case SOME(exp) then
let &varInits += 'alloc_<%expTypeShort(var.ty)%>_array(&<%varName%>, <%listLength(instDims)%>, <%instDimsInit%>);<%\n%>'
let defaultValue = varDefaultValue(var, "", i, varName, &varDecls, &varInits)
let &varInits += defaultValue
let defaultValue1 = '<%varName%> = <%daeExp(exp, contextFunction, &varInits /*BUFC*/, &varDecls /*BUFD*/)%>;<%\n%>'
let &varInits += defaultValue1
" "
else
let &varInits += 'alloc_<%expTypeShort(var.ty)%>_array(&<%varName%>, <%listLength(instDims)%>, <%instDimsInit%>);<%\n%>'
let defaultValue = varDefaultValue(var, "", i, varName, &varDecls, &varInits)
let &varInits += defaultValue
"")
else
(match var.value
case SOME(exp) then
let defaultValue = '<%varName%> = <%daeExp(exp, contextFunction, &varInits /*BUFC*/, &varDecls /*BUFD*/)%>;<%\n%>'
let &varInits += defaultValue

" "
else
"")

case var as FUNCTION_PTR(__) then
let &ignore = buffer ""
let &varDecls += functionArg(var,&ignore)
""
else error(sourceInfo(), 'Unknown local variable type in record')
end varInitRecord;

template functionBodyBoxed(Function fn)
"Generates code for a boxed version of a function. Extracts the needed data
from a function and calls functionBodyBoxedImpl"
Expand Down
1 change: 1 addition & 0 deletions Compiler/Template/SimCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ package SimCode
record RECORD_CONSTRUCTOR
Absyn.Path name;
list<Variable> funArgs;
list<Variable> locals;
Absyn.Info info;
end RECORD_CONSTRUCTOR;
end Function;
Expand Down

0 comments on commit cae0afd

Please sign in to comment.