Skip to content

Commit

Permalink
Emitting functions to return references to static fields for FMU export.
Browse files Browse the repository at this point in the history
	modified:   src/main/java/org/overture/codegen/vdm2c/transformations/CreateGlobalConstInitFunctionTrans.java
	modified:   src/main/java/org/overture/codegen/vdm2c/transformations/CreateGlobalStaticInitFunctionTrans.java
  • Loading branch information
Victor Bandur committed Jul 8, 2016
1 parent 686e5e4 commit 8536610
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Expand Up @@ -17,7 +17,7 @@
import org.overture.codegen.trans.assistants.TransAssistantIR;

public class CreateGlobalConstInitFunctionTrans extends
DepthFirstAnalysisCAdaptor
DepthFirstAnalysisCAdaptor
{
private static final String GLOBAL_CONST_INIT_FUNCTION_PATTERN = "%s_const_init";
private static final String GLOBAL_CONST_SHUTDOWN_FUNCTION_PATTERN = "%s_const_shutdown";
Expand All @@ -31,15 +31,15 @@ public CreateGlobalConstInitFunctionTrans(TransAssistantIR assist)
void createInitMethod(ADefaultClassDeclIR node) throws AnalysisException
{
ABlockStmIR body = new ABlockStmIR();

for (AFieldDeclIR field : node.getFields())
{
if (field.getFinal() && field.getInitial() != null)
{
body.getStatements().add(newAssignment(newIdentifier(field.getName(), null), field.getInitial()));
}
}

//In case there is nothing to initialize, we still want the functions to have a body. See comment below.
body.getStatements().add(new AReturnStmIR());

Expand All @@ -61,7 +61,7 @@ void createShutdownMethod(ADefaultClassDeclIR node)
body.getStatements().add(toStm(newApply("vdmFree", newIdentifier(field.getName(), null))));
}
}

//In case there is nothing to initialize, we still want the functions to have a body. See comment below.
body.getStatements().add(new AReturnStmIR());

Expand Down
Expand Up @@ -4,6 +4,8 @@
import static org.overture.codegen.vdm2c.utils.CTransUtil.newAssignment;
import static org.overture.codegen.vdm2c.utils.CTransUtil.newIdentifier;
import static org.overture.codegen.vdm2c.utils.CTransUtil.newInternalMethod;
import static org.overture.codegen.vdm2c.utils.CTransUtil.newReturnStm;
import static org.overture.codegen.vdm2c.utils.CTransUtil.newTvpType;
import static org.overture.codegen.vdm2c.utils.CTransUtil.toStm;

import org.overture.cgc.extast.analysis.DepthFirstAnalysisCAdaptor;
Expand All @@ -13,14 +15,16 @@
import org.overture.codegen.ir.declarations.AMethodDeclIR;
import org.overture.codegen.ir.statements.ABlockStmIR;
import org.overture.codegen.ir.statements.AReturnStmIR;
import org.overture.codegen.ir.types.AExternalTypeIR;
import org.overture.codegen.ir.types.AVoidTypeIR;
import org.overture.codegen.trans.assistants.TransAssistantIR;

public class CreateGlobalStaticInitFunctionTrans extends
DepthFirstAnalysisCAdaptor
DepthFirstAnalysisCAdaptor
{
private static final String GLOBAL_STATIC_INIT_FUNCTION_PATTERN = "%s_static_init";
private static final String GLOBAL_STATIC_SHUTDOWN_FUNCTION_PATTERN = "%s_static_shutdown";
private static final String GLOBAL_REFERENCE_RETURN_FUNCTION_PATTERN = "get_%s";
public TransAssistantIR assist;

public CreateGlobalStaticInitFunctionTrans(TransAssistantIR assist)
Expand All @@ -43,7 +47,7 @@ void createInitMethod(ADefaultClassDeclIR node) throws AnalysisException

//In case there is nothing to initialize, we still want the functions to have a body. See comment below.
body.getStatements().add(new AReturnStmIR());

//Emit init function even if no static fields are present. Simplifies FMU export.
AMethodDeclIR method = newInternalMethod(String.format(GLOBAL_STATIC_INIT_FUNCTION_PATTERN, node.getName()), body, new AVoidTypeIR(), false);
method.setAccess("public");
Expand All @@ -65,19 +69,44 @@ void createShutdownMethod(ADefaultClassDeclIR node)

//In case there is nothing to initialize, we still want the functions to have a body. See comment below.
body.getStatements().add(new AReturnStmIR());

//Emit shutdown function even if no static fields are present. Simplifies FMU export.
AMethodDeclIR method = newInternalMethod(String.format(GLOBAL_STATIC_SHUTDOWN_FUNCTION_PATTERN, node.getName()), body, new AVoidTypeIR(), false);
method.setAccess("public");
node.getMethods().add(method);
}

void createGetReferenceMethods(ADefaultClassDeclIR node) throws AnalysisException
{
AMethodDeclIR method;
ABlockStmIR body;

for (AFieldDeclIR field : node.getFields())
{
if (!field.getFinal() && field.getStatic())
{
body = new ABlockStmIR();
body.getStatements().add(newReturnStm(newIdentifier(field.getName(), null)));

//Emit init function even if no value fields are present. Simplifies FMU export.
method = newInternalMethod(String.format(GLOBAL_REFERENCE_RETURN_FUNCTION_PATTERN, field.getName()), body, newTvpType(), false);

//In case there is nothing to initialize, we still want the functions to have a body. See comment below.
body.getStatements().add(new AReturnStmIR());

method.setAccess("public");
node.getMethods().add(method);
}
}
}

@Override
public void caseADefaultClassDeclIR(ADefaultClassDeclIR node)
throws AnalysisException
{
createInitMethod(node);
createShutdownMethod(node);
createGetReferenceMethods(node);
}

}

0 comments on commit 8536610

Please sign in to comment.