Skip to content

Commit

Permalink
Fix #8131 (#8152)
Browse files Browse the repository at this point in the history
- Add flag `-d=printRecordTypes` to print out record types instead of
  record constructor functions in the flat code.
  • Loading branch information
perost committed Nov 16, 2021
1 parent 8df2386 commit d8aa7d6
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 19 deletions.
48 changes: 30 additions & 18 deletions OMCompiler/Compiler/FrontEnd/DAEDump.mo
Expand Up @@ -1258,15 +1258,21 @@ algorithm
case DAE.RECORD_CONSTRUCTOR(path = fpath,type_=t)
equation
false = Flags.isSet(Flags.DISABLE_RECORD_CONSTRUCTOR_OUTPUT);
Print.printBuf("function ");
fstr = AbsynUtil.pathStringNoQual(fpath);
Print.printBuf(fstr);
Print.printBuf(" \"Automatically generated record constructor for "+fstr+"\"\n");
Print.printBuf(printRecordConstructorInputsStr(t));
Print.printBuf(" output "+AbsynUtil.pathLastIdent(fpath)+ " res;\n");
Print.printBuf("end ");
Print.printBuf(fstr);
Print.printBuf(";\n\n");

if Flags.isSet(Flags.PRINT_RECORD_TYPES) then
Print.printBuf(Types.unparseType(t));
Print.printBuf("\n");
else
Print.printBuf("function ");
fstr = AbsynUtil.pathStringNoQual(fpath);
Print.printBuf(fstr);
Print.printBuf(" \"Automatically generated record constructor for "+fstr+"\"\n");
Print.printBuf(printRecordConstructorInputsStr(t));
Print.printBuf(" output "+AbsynUtil.pathLastIdent(fpath)+ " res;\n");
Print.printBuf("end ");
Print.printBuf(fstr);
Print.printBuf(";\n\n");
end if;
then
();

Expand Down Expand Up @@ -3633,15 +3639,21 @@ algorithm
case (DAE.RECORD_CONSTRUCTOR(path = fpath,type_=tp), str)
equation
false = Flags.isSet(Flags.DISABLE_RECORD_CONSTRUCTOR_OUTPUT);
fstr = AbsynUtil.pathStringNoQual(fpath);
str = IOStream.append(str, "function ");
str = IOStream.append(str, fstr);
str = IOStream.append(str, " \"Automatically generated record constructor for " + fstr + "\"\n");
str = IOStream.append(str, printRecordConstructorInputsStr(tp));
str = IOStream.append(str, " output "+AbsynUtil.pathLastIdent(fpath) + " res;\n");
str = IOStream.append(str, "end ");
str = IOStream.append(str, fstr);
str = IOStream.append(str, ";\n\n");

if Flags.isSet(Flags.PRINT_RECORD_TYPES) then
str = IOStream.append(str, Types.unparseType(tp));
str = IOStream.append(str, "\n");
else
fstr = AbsynUtil.pathStringNoQual(fpath);
str = IOStream.append(str, "function ");
str = IOStream.append(str, fstr);
str = IOStream.append(str, " \"Automatically generated record constructor for " + fstr + "\"\n");
str = IOStream.append(str, printRecordConstructorInputsStr(tp));
str = IOStream.append(str, " output "+AbsynUtil.pathLastIdent(fpath) + " res;\n");
str = IOStream.append(str, "end ");
str = IOStream.append(str, fstr);
str = IOStream.append(str, ";\n\n");
end if;
then
str;

Expand Down
8 changes: 8 additions & 0 deletions OMCompiler/Compiler/Template/DAEDumpTV.mo
Expand Up @@ -1471,11 +1471,19 @@ end System;

package Flags
uniontype ConfigFlag end ConfigFlag;
uniontype DebugFlag end DebugFlag;
constant ConfigFlag MODELICA_OUTPUT;
constant DebugFlag PRINT_RECORD_TYPES;

function getConfigBool
input ConfigFlag inFlag;
output Boolean outValue;
end getConfigBool;

function isSet
input DebugFlag inFlag;
output Boolean outValue;
end isSet;
end Flags;

end DAEDumpTV;
19 changes: 19 additions & 0 deletions OMCompiler/Compiler/Template/DAEDumpTpl.tpl
Expand Up @@ -79,6 +79,11 @@ template dumpFunction(DAE.Function function)
end <%AbsynDumpTpl.dumpPathNoQual(path)%>;
>>
case RECORD_CONSTRUCTOR(__) then
if (Flags.isSet(Flags.PRINT_RECORD_TYPES)) then
<<
<%dumpRecordType(type_)%>
>>
else
<<
function <%AbsynDumpTpl.dumpPathNoQual(path)%> "Automatically generated record constructor for <%AbsynDumpTpl.dumpPathNoQual(path)%>"
<%dumpRecordInputVarStr(type_)%>
Expand Down Expand Up @@ -383,6 +388,20 @@ match arg
'<%ty_str%> <%c_str%><%p_str%><%name%><%binding_str%>'
end dumpFuncArg;

template dumpRecordType(Type ty)
::=
match ty
case T_COMPLEX(__) then
let name = AbsynDumpTpl.dumpPath(ClassInf.getStateName(complexClassType))
let vars = dumpRecordVars(varLst)
<<
record <%name%>
<%vars%>
end <%name%>;
>>
case T_FUNCTION(__) then dumpRecordType(funcResultType)
end dumpRecordType;

template dumpConst(Const c)
::=
match c
Expand Down
2 changes: 2 additions & 0 deletions OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -545,6 +545,8 @@ constant DebugFlag ZMQ_LISTEN_TO_ALL = DEBUG_FLAG(186, "zmqDangerousAcceptConnec
Gettext.gettext("When opening a zmq connection, listen on all interfaces instead of only connections from 127.0.0.1."));
constant DebugFlag DUMP_CONVERSION_RULES = DEBUG_FLAG(187, "dumpConversionRules", false,
Gettext.gettext("Dumps the rules when converting a package using a conversion script."));
constant DebugFlag PRINT_RECORD_TYPES = DEBUG_FLAG(188, "printRecordTypes", false,
Gettext.gettext("Prints out record types as part of the flat code."));

public
// CONFIGURATION FLAGS
Expand Down
3 changes: 2 additions & 1 deletion OMCompiler/Compiler/Util/FlagsUtil.mo
Expand Up @@ -243,7 +243,8 @@ constant list<Flags.DebugFlag> allDebugFlags = {
Flags.ARRAY_CONNECT,
Flags.COMBINE_SUBSCRIPTS,
Flags.ZMQ_LISTEN_TO_ALL,
Flags.DUMP_CONVERSION_RULES
Flags.DUMP_CONVERSION_RULES,
Flags.PRINT_RECORD_TYPES
};

protected
Expand Down
1 change: 1 addition & 0 deletions testsuite/flattening/modelica/scodeinst/Makefile
Expand Up @@ -810,6 +810,7 @@ PartialType3.mo \
Prefix1.mo \
Prefix2.mo \
Prefix3.mo \
PrintRecordTypes1.mo \
PropagateExtends.mo \
RangeInvalidStep1.mo \
RangeInvalidStep2.mo \
Expand Down
52 changes: 52 additions & 0 deletions testsuite/flattening/modelica/scodeinst/PrintRecordTypes1.mo
@@ -0,0 +1,52 @@
// name: PrintRecordTypes1
// keywords:
// status: correct
// cflags: -d=newInst,-nfScalarize,printRecordTypes
//

record R1
Real x;
Real y;
end R1;

record R2
R1 ra;
R1 rb;
end R2;

model B
R2 c[10];
end B;

model PrintRecordTypes1
B[100] b;
equation
for i in 1:100 loop
b[i].c[10] = R2(R1(1, 0), R1(i,1));
b[i].c[1].rb = R1(i,0);
end for;
end PrintRecordTypes1;

// Result:
// record R1
// input Real x;
// input Real y;
// end R1;
//
// record R2
// input R1 ra;
// input R1 rb;
// end R2;
//
// class PrintRecordTypes1
// Real[100, 10] b.c.ra.x;
// Real[100, 10] b.c.ra.y;
// Real[100, 10] b.c.rb.x;
// Real[100, 10] b.c.rb.y;
// equation
// for i in 1:100 loop
// b[i].c[1].rb = R1(/*Real*/(i), 0.0);
// b[i].c[10] = R2(R1(1.0, 0.0), R1(/*Real*/(i), 1.0));
// end for;
// end PrintRecordTypes1;
// endResult

0 comments on commit d8aa7d6

Please sign in to comment.