Skip to content

Commit

Permalink
[Flat] Fix output of external functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Jun 4, 2020
1 parent 5ab5bb3 commit 8ee6976
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 20 deletions.
15 changes: 15 additions & 0 deletions OMCompiler/Compiler/FrontEnd/SCodeDump.mo
Expand Up @@ -63,6 +63,21 @@ end SCodeDumpOptions;

public constant SCodeDumpOptions defaultOptions = OPTIONS(false,false,false,false,true,true,false,false,false);

function generateOptions
input Boolean stripAlgorithmSections = false;
input Boolean stripProtectedImports = false;
input Boolean stripProtectedClasses = false;
input Boolean stripProtectedComponents = false;
input Boolean stripMetaRecords = true;
input Boolean stripGraphicalAnnotations = true;
input Boolean stripStringComments = false;
input Boolean stripExternalDecl = false;
input Boolean stripOutputBindings = false;
output SCodeDumpOptions options;
algorithm
options := OPTIONS(stripAlgorithmSections, stripProtectedImports, stripProtectedClasses, stripProtectedComponents, stripMetaRecords,stripGraphicalAnnotations, stripStringComments, stripExternalDecl, stripOutputBindings);
end generateOptions;

public function programStr
input SCode.Program inProgram;
input SCodeDumpOptions options = defaultOptions;
Expand Down
6 changes: 4 additions & 2 deletions OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -486,8 +486,10 @@ public
algorithm
types := ClassTree.foldComponents(Class.classTree(InstNode.getClass(fn.node)),
collectComponentFlatTypes, types);
body := Function.getBody(fn);
types := List.fold(body, collectStatementFlatTypes, types);
if not Function.isExternal(fn) then
body := Function.getBody(fn);
types := List.fold(body, collectStatementFlatTypes, types);
end if;
end collectFunctionFlatTypes;

function collectComponentFlatTypes
Expand Down
31 changes: 13 additions & 18 deletions OMCompiler/Compiler/NFFrontEnd/NFFunction.mo
Expand Up @@ -713,12 +713,7 @@ uniontype Function
end for;
end if;

fn_body := getBody(fn);

if not listEmpty(fn_body) then
s := IOStream.append(s, "algorithm\n");
s := Statement.toFlatStreamList(fn_body, " ", s);
end if;
s := Sections.toFlatStream(InstNode.getSections(fn.node), s);

s := IOStream.append(s, "end '");
s := IOStream.append(s, fn_name);
Expand Down Expand Up @@ -2199,26 +2194,26 @@ protected
input InstNode node;
output list<Statement> body;
protected
Class cls = InstNode.getClass(node);
Algorithm fn_body;
algorithm
body := match cls
case Class.INSTANCED_CLASS(sections = Sections.SECTIONS(algorithms = {fn_body})) then fn_body.statements;
case Class.INSTANCED_CLASS(sections = Sections.EMPTY()) then {};
body := match InstNode.getSections(node)
case Sections.SECTIONS(algorithms = {}) then {};
case Sections.SECTIONS(algorithms = {fn_body}) then fn_body.statements;
case Sections.EMPTY() then {};
case Sections.EXTERNAL()
algorithm
Error.assertion(false, getInstanceName() + " got function with external section (not algorithm section)", sourceInfo());
then fail();

case Class.INSTANCED_CLASS(sections = Sections.SECTIONS(algorithms = _ :: _))
case Sections.SECTIONS()
algorithm
Error.assertion(false, getInstanceName() + " got function with multiple algorithm sections", sourceInfo());
then
fail();

case Class.TYPED_DERIVED() then getBody2(cls.baseClass);
then fail();

else
algorithm
Error.assertion(false, getInstanceName() + " got unknown function", sourceInfo());
then
fail();
Error.assertion(false, getInstanceName() + " got unknown sections", sourceInfo());
then fail();

end match;
end getBody2;
Expand Down
18 changes: 18 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFInstNode.mo
Expand Up @@ -38,6 +38,7 @@ import Absyn;
import AbsynUtil;
import Type = NFType;
import NFFunction.Function;
import Sections = NFSections;
import Pointer;
import Error;
import Prefixes = NFPrefixes;
Expand Down Expand Up @@ -1693,6 +1694,23 @@ uniontype InstNode
end match;
end hasBinding;

function getSections
input InstNode node;
output Sections sections;
protected
Class cls = InstNode.getClass(node);
algorithm
sections := match cls
case Class.INSTANCED_CLASS() then cls.sections;
case Class.TYPED_DERIVED() then getSections(cls.baseClass);

else
algorithm
Error.assertion(false, getInstanceName() + " did not get an instanced class", sourceInfo());
then fail();
end match;
end getSections;

end InstNode;

annotation(__OpenModelica_Interface="frontend");
Expand Down
38 changes: 38 additions & 0 deletions OMCompiler/Compiler/NFFrontEnd/NFSections.mo
Expand Up @@ -39,6 +39,7 @@ encapsulated uniontype NFSections

protected
import Sections = NFSections;
import IOStream;

public
record SECTIONS
Expand Down Expand Up @@ -366,5 +367,42 @@ public
end match;
end isEmpty;

function toFlatStream
input Sections sections;
input output IOStream.IOStream s;
algorithm
() := match sections
case SECTIONS()
algorithm
for alg in sections.algorithms loop
s := IOStream.append(s, "algorithm\n");
s := Statement.toFlatStreamList(alg.statements, " ", s);
end for;
then ();
case EXTERNAL()
algorithm
s := IOStream.append(s, "external \"");
s := IOStream.append(s, sections.language);
s := IOStream.append(s, "\"");
if sections.explicit then
if not ComponentRef.isEmpty(sections.outputRef) then
s := IOStream.append(s, " ");
s := IOStream.append(s, ComponentRef.toFlatString(sections.outputRef));
s := IOStream.append(s, " =");
end if;
s := IOStream.append(s, " ");
s := IOStream.append(s, sections.name);
s := IOStream.append(s, "(");
s := IOStream.append(s, stringDelimitList(list(Expression.toFlatString(e) for e in sections.args), ", "));
s := IOStream.append(s, ")");
end if;
if isSome(sections.ann) then
s := IOStream.append(s, SCodeDump.printAnnotationStr(SCode.COMMENT(sections.ann, NONE())));
end if;
s := IOStream.append(s, ";\n");
then ();
end match;
end toFlatStream;

annotation(__OpenModelica_Interface="frontend");
end NFSections;

0 comments on commit 8ee6976

Please sign in to comment.