Skip to content

Commit

Permalink
Merge branch 'pvj/ovt-254' of github.com:overturetool/vdm2c into pvj/…
Browse files Browse the repository at this point in the history
…ovt-254
  • Loading branch information
bandurvp committed Nov 9, 2017
2 parents 87740a5 + 3679031 commit 184bcfd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
import org.overture.codegen.ir.SPatternIR;
import org.overture.codegen.ir.SStmIR;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.expressions.AApplyExpIR;
import org.overture.codegen.ir.expressions.AGreaterEqualNumericBinaryExpIR;
import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
import org.overture.codegen.ir.expressions.ALessEqualNumericBinaryExpIR;
import org.overture.codegen.ir.expressions.ALessNumericBinaryExpIR;
import org.overture.codegen.ir.expressions.APlusNumericBinaryExpIR;
import org.overture.codegen.ir.expressions.SNumericBinaryExpIR;
import org.overture.codegen.ir.expressions.*;
import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
import org.overture.codegen.ir.statements.ABlockStmIR;
import org.overture.codegen.ir.statements.AForAllStmIR;
Expand Down Expand Up @@ -146,7 +140,12 @@ void rewritetoCollectionLoop(INode source, SExpIR set,
String setName = getNewName();
String indexName = getNewName();
replBlock.getLocalDefs().add(newDeclarationAssignment(setName, newTvpType(), set, set.getSourceNode()));
replBlock.getLocalDefs().add(newDeclarationAssignment(indexName, newExternalType("int"), createIdentifier("0", null), null));

AExternalExpIR zero = new AExternalExpIR();
zero.setTargetLangExp("0");
zero.setType(newExternalType("int"));

replBlock.getLocalDefs().add(newDeclarationAssignment(indexName, newExternalType("int"), zero, null));
replBlock.getStatements().add(toStm(newMacroApply("ASSERT_CHECK_COLLECTION", newIdentifier(setName, set.getSourceNode()))));
String colName = getNewName();
replBlock.getStatements().add(toStm(newMacroApply("UNWRAP_COLLECTION", newIdentifier(colName, set.getSourceNode()), newIdentifier(setName, set.getSourceNode()))));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.overture.codegen.vdm2c.transformations;

import java.util.HashMap;
import java.util.LinkedList;

import org.overture.cgc.extast.analysis.DepthFirstAnalysisCAdaptor;
import org.overture.codegen.ir.INode;
import org.overture.codegen.ir.SExpIR;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.declarations.AMethodDeclIR;
import org.overture.codegen.ir.expressions.AApplyExpIR;
import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
import org.overture.codegen.ir.statements.AReturnStmIR;
import org.overture.codegen.trans.assistants.TransAssistantIR;
import org.overture.codegen.vdm2c.CForIterator;
import org.overture.codegen.vdm2c.ColTrans;
Expand All @@ -23,6 +22,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.LinkedList;

public class GarbageCollectionTrans extends DepthFirstAnalysisCAdaptor
{
final static Logger logger = LoggerFactory.getLogger(GarbageCollectionTrans.class);
Expand Down Expand Up @@ -166,11 +168,6 @@ public void caseAMacroApplyExpIR(AMacroApplyExpIR node)
// Used to handle GET and SET macros
super.caseAMacroApplyExpIR(node);

if(insideFieldInitializer(node))
{
return;
}

changeToGcCall(node, node.getArgs(), node.getRoot());
}

Expand All @@ -179,16 +176,23 @@ public void caseAApplyExpIR(AApplyExpIR node) throws AnalysisException
{
super.caseAApplyExpIR(node);

if(insideFieldInitializer(node))
changeToGcCall(node, node.getArgs(), node.getRoot());
}

@Override
public void caseAReturnStmIR(AReturnStmIR node) throws AnalysisException {
super.caseAReturnStmIR(node);

if(isInsideFieldInitializer(node))
{
return;
SExpIR exp = node.getExp();
SExpIR cloned = ValueSemantics.forceClone(exp.clone());
assist.replaceNodeWith(exp, cloned);
}

changeToGcCall(node, node.getArgs(), node.getRoot());
}

private void changeToGcCall(SExpIR node, LinkedList<SExpIR> args,
SExpIR root)
SExpIR root)
{
if (root instanceof AIdentifierVarExpIR)
{
Expand Down Expand Up @@ -262,21 +266,19 @@ private boolean isSetVarToGrow(String name)
{
return name.equals(CSetCompStrategy.NEW_SET_VAR_TO_GROW);
}

private boolean insideFieldInitializer(SExpIR exp)
{
AMethodDeclIR encMethod = exp.getAncestor(AMethodDeclIR.class);

if (encMethod != null)
{
private boolean isInsideFieldInitializer(INode node) {
AMethodDeclIR encMethod = node.getAncestor(AMethodDeclIR.class);

if (encMethod != null) {
Object tag = encMethod.getTag();

if(tag instanceof Vdm2cTag)
{

if (tag instanceof Vdm2cTag) {
Vdm2cTag t = (Vdm2cTag) tag;
return t.methodTags.contains(MethodTag.FieldInitializer);
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ public class ValueSemantics {

public static SExpIR clone(SExpIR exp) {
if (exp instanceof SVarExpIR) {
AApplyExpIR vdmCloneApply = CTransUtil.newApply(VDM_CLONE, exp);

if (exp.getType() != null) {
vdmCloneApply.setType(exp.getType().clone());
}
return vdmCloneApply;
return forceClone(exp);
} else {
// Don't clone literals
return exp;
}
}

public static SExpIR forceClone(SExpIR exp)
{
AApplyExpIR vdmCloneApply = CTransUtil.newApply(VDM_CLONE, exp);

if (exp.getType() != null) {
vdmCloneApply.setType(exp.getType().clone());
}
return vdmCloneApply;
}

public static SExpIR free(String name, SourceNode source)
{
AIdentifierVarExpIR newIdentifier = CTransUtil.newIdentifier(name, source);
Expand Down

0 comments on commit 184bcfd

Please sign in to comment.