Skip to content

Commit db37146

Browse files
committed
Fix context of function Cpp calls (#3742)
1 parent 1916068 commit db37146

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

Compiler/Template/CodegenCppCommon.tpl

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ template contextCref2(ComponentRef cr, Context context)
162162
else ""
163163
end contextCref2;
164164

165+
template contextFunName(String funName, Context context)
166+
"Generates a name in the Functions object depending on the context we're in."
167+
::=
168+
match context
169+
case FUNCTION_CONTEXT(__) then '<%funName%>'
170+
else '_functions-><%funName%>'
171+
end contextFunName;
172+
165173
template crefWithIndex(ComponentRef cr, Context context, Text &varDecls, SimCode simCode, Text& extraFuncs, Text& extraFuncsDecl,
166174
Text extraFuncsNamespace, Text stateDerVectorName /*=__zDot*/, Boolean useFlatArrayNotation)
167175
"Return cref with index for the lhs of a for loop, i.e., _resistori_P_i."
@@ -542,7 +550,7 @@ case T_COMPLEX(complexClassType = record_state, varLst = var_lst) then
542550
let record_type_name = underscorePath(ClassInf.getStateName(record_state))
543551
let ret_type = '<%record_type_name%>RetType'
544552
let ret_var = tempDecl(ret_type, &varDecls)
545-
let &preExp += '_functions-><%record_type_name%>(<%vars%>,<%ret_var%>);<%\n%>/*testfunction*/'
553+
let &preExp += '<%contextFunName(record_type_name, context)%>(<%vars%>,<%ret_var%>);<%\n%>/*testfunction*/'
546554
'<%ret_var%>'
547555
end daeExpRecordCrefRhs;
548556

@@ -2144,10 +2152,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
21442152
case exp as CALL(attr=attr as CALL_ATTR(ty=T_NORETCALL(__))) then
21452153
let argStr = (expLst |> exp => '<%daeExp(exp, context, &preExp /*BUFC*/, &varDecls /*BUFD*/,simCode , &extraFuncs , &extraFuncsDecl, extraFuncsNamespace, stateDerVectorName, useFlatArrayNotation)%>' ;separator=", ")
21462154
let funName = '<%underscorePath(path)%>'
2147-
let &preExp += match context
2148-
case FUNCTION_CONTEXT(__) then '<%funName%>(<%argStr%>);<%\n%>'
2149-
/*multi_array else 'assign_array(<%retVar%> ,_functions.<%funName%>(<%argStr%>));<%\n%>'*/
2150-
else '_functions-><%funName%>(<%argStr%>);<%\n%>'
2155+
let &preExp += '<%contextFunName(funName, context)%>(<%argStr%>);<%\n%>'
21512156
""
21522157
/*Function calls with array return type*/
21532158
case exp as CALL(attr=attr as CALL_ATTR(ty=T_ARRAY(ty=ty,dims=dims))) then
@@ -2156,13 +2161,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
21562161
let funName = '<%underscorePath(path)%>'
21572162
let retType = '<%funName%>RetType /* undefined */'
21582163
let retVar = tempDecl(retType, &varDecls)
2159-
let &preExp += match context
2160-
case FUNCTION_CONTEXT(__) then '(<%funName%>(<%argStr%><%if expLst then if retVar then "," %><%retVar%>));<%\n%>/*funccall*/'
2161-
/*multi_array else 'assign_array(<%retVar%> ,_functions.<%funName%>(<%argStr%>));<%\n%>'*/
2162-
else '_functions-><%funName%>(<%argStr%><%if expLst then if retVar then "," %><%retVar%>);<%\n%>'
2163-
2164-
2165-
2164+
let &preExp += '<%contextFunName(funName, context)%>(<%argStr%><%if expLst then if retVar then "," %><%retVar%>);<%\n%>'
21662165
'<%retVar%>'
21672166
/*Function calls with tuple return type
21682167
case exp as CALL(attr=attr as CALL_ATTR(ty=T_TUPLE(__))) then
@@ -2183,7 +2182,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/, Text &varD
21832182
let retType = '<%funName%>RetType /* undefined */'
21842183
let retVar = tempDecl(retType, &varDecls)
21852184
let &preExp += match context case FUNCTION_CONTEXT(__) then'<%funName%>(<%argStr%><%if explist then if retVar then "," %><%if retVar then '<%retVar%>'%>);<%\n%>'
2186-
else '_functions-><%funName%>(<%argStr%><%if explist then if retVar then "," %> <%if retVar then '<%retVar%>'%>);<%\n%>'
2185+
else '<%contextFunName(funName, context)%>(<%argStr%><%if explist then if retVar then "," %> <%if retVar then '<%retVar%>'%>);<%\n%>'
21872186
'<%retVar%>'
21882187

21892188
end daeExpCall;
@@ -2968,7 +2967,8 @@ template daeExpCallTuple(Exp call , Text additionalOutputs/* arguments 2..N */,
29682967
//'<%name%>(<%argStr%><%additionalOutputs%>)'
29692968
'/*Closure?*/<%closure%> ? (<%typeCast1%> <%func%>) (<%argStrPointer%><%additionalOutputs%>) : (<%typeCast2%> <%func%>) (<%argStr%><%additionalOutputs%>)'
29702969
else
2971-
'_functions-><%underscorePath(path)%>(<%argStr%>,<%additionalOutputs%>)'
2970+
let name = underscorePath(path)
2971+
'<%contextFunName(name, context)%>(<%argStr%>,<%additionalOutputs%>)'
29722972
end daeExpCallTuple;
29732973

29742974
template generateTypeCast(Type ty, list<DAE.Exp> es, Boolean isClosure, Text &preExp /*BUFP*/,
@@ -2987,10 +2987,8 @@ template daeExpSharedLiteral(Exp exp, Context context, Text &preExp /*BUFP*/, Te
29872987
"Generates code for a match expression."
29882988
::=
29892989
match exp case exp as SHARED_LITERAL(__) then
2990-
match context case FUNCTION_CONTEXT(__) then
2991-
' _OMC_LIT<%exp.index%>'
2992-
else
2993-
'_functions->_OMC_LIT<%exp.index%>'
2990+
let lit = '_OMC_LIT<%exp.index%>'
2991+
'<%contextFunName(lit, context)%>'
29942992
end daeExpSharedLiteral;
29952993

29962994

0 commit comments

Comments
 (0)