Skip to content

Commit

Permalink
Emit type encoding for is_* checks #108
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwvj committed Oct 26, 2017
1 parent f906806 commit ae81a7f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions c/vdmclib/src/main/VdmBasicTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ TVP isRecord(TVP val, int recID);
TVP isRecordGC(TVP val, int recID, TVP *from);
#endif
TVP is(TVP v, char ot[]);
TVP isGC(TVP v, char ot[], TVP *from);
TVP sameClass(TVP a, TVP b);
TVP sameClassGC(TVP a, TVP b, TVP *from);
#endif /* NO_IS */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public void initNames()
gcNames.put(IsCheckTrans.VDM_IS_RAT, "isRatGC");
gcNames.put(IsCheckTrans.VDM_IS_CHAR, "isCharGC");
gcNames.put(IsCheckTrans.VDM_IS_TOKEN, "isTokenGC");
gcNames.put(IsCheckTrans.VDM_IS, "isGC");

// Numeric comparison
gcNames.put(NumericTrans.VDM_GREATER_THAN, "vdmGreaterThanGC");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package org.overture.codegen.vdm2c.transformations;

import org.apache.log4j.Logger;
import org.overture.cgc.extast.analysis.DepthFirstAnalysisCAdaptor;
import org.overture.codegen.ir.SStmIR;
import org.overture.codegen.ir.STypeIR;
import org.overture.codegen.ir.analysis.AnalysisException;
import org.overture.codegen.ir.analysis.intf.IAnalysis;
import org.overture.codegen.ir.declarations.AVarDeclIR;
import org.overture.codegen.ir.expressions.*;
import org.overture.codegen.ir.statements.ABlockStmIR;
import org.overture.codegen.ir.types.*;
import org.overture.codegen.trans.assistants.TransAssistantIR;
import org.overture.codegen.vdm2c.extast.statements.ALocalVariableDeclarationStmIR;
import org.overture.codegen.vdm2c.utils.CTransUtil;
import org.overture.codegen.vdm2c.utils.IApplyAssistant;

public class IsCheckTrans extends DepthFirstAnalysisCAdaptor implements IApplyAssistant {

protected Logger log = Logger.getLogger(this.getClass().getName());

public static final String VDM_IS_NAT = "isNat";
public static final String VDM_IS_NAT1 = "isNat1";
public static final String VDM_IS_INT = "isInt";
Expand All @@ -20,6 +27,9 @@ public class IsCheckTrans extends DepthFirstAnalysisCAdaptor implements IApplyAs
public static final String VDM_IS_RAT = "isRat";
public static final String VDM_IS_CHAR = "isChar";
public static final String VDM_IS_TOKEN = "isToken";
public static final String VDM_IS = "is";

public static final String IS_EXP_ENCODING = "isExpEncoding_";

private TransAssistantIR assist;

Expand Down Expand Up @@ -65,6 +75,38 @@ else if(type instanceof ATokenBasicTypeIR)
{
CTransUtil.rewriteToApply(this, node, VDM_IS_TOKEN, node.getExp());
}
else
{
String enc = IsExpTypeEncoder.encodeType(type);

if(enc == null)
{
log.error("Could not encode type!");
return;
}

AExternalExpIR encodingExp = new AExternalExpIR();
encodingExp.setTargetLangExp(enc);

AExternalTypeIR charArr = new AExternalTypeIR();
charArr.setName("char");

ABlockStmIR replacement = new ABlockStmIR();

String varName = assist.getInfo().getTempVarNameGen().nextVarName(IS_EXP_ENCODING);
AVarDeclIR encodingVar = assist.consDecl(varName + "[]", charArr, encodingExp);
ALocalVariableDeclarationStmIR declStm = new ALocalVariableDeclarationStmIR();
declStm.setDecleration(encodingVar);
replacement.getStatements().add(declStm);
AIdentifierVarExpIR id = assist.getInfo().getExpAssistant().consIdVar(varName, encodingVar.getType().clone());

SStmIR stm = assist.getEnclosingStm(node, "is expression");

CTransUtil.rewriteToApply(this, node, VDM_IS, node.getExp(), id);

assist.replaceNodeWith(stm, replacement);
replacement.getStatements().add(stm);
}
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions core/vdm2c/src/test/resources/native/is/ExpressionsIs_Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,14 @@ TEST_F(TestFlowFunctions, isTokenPos)
TEST_F(TestFlowFunctions, isTokenNeg)
{
CHECK(CLASS_IsTest__Z10isTokenNegEV);
}

TEST_F(TestFlowFunctions, isNatCharTuplePos)
{
CHECK(CLASS_IsTest__Z17isNatCharTuplePosEV);
}

TEST_F(TestFlowFunctions, isNatCharTupleNeg)
{
CHECK(CLASS_IsTest__Z17isNatCharTupleNegEV);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ public isTokenNeg : () ==> bool
isTokenNeg () ==
return not is_token("test");

public isNatCharTuplePos : () ==> bool
isNatCharTuplePos () ==
return is_(mk_(0,'a'), nat * char);

public isNatCharTupleNeg : () ==> bool
isNatCharTupleNeg () ==
return not is_(mk_(-1,'a'), nat * char);

end IsTest

0 comments on commit ae81a7f

Please sign in to comment.