Package: TargetNormaliserTrans

TargetNormaliserTrans

nameinstructionbranchcomplexitylinemethod
TargetNormaliserTrans(JmlGenerator)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
caseAFieldExpIR(AFieldExpIR)
M: 0 C: 17
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
caseAMapSeqUpdateStmIR(AMapSeqUpdateStmIR)
M: 5 C: 5
50%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
getStateDesInfo()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
markAsCloneFree(List)
M: 0 C: 19
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
normaliseTarget(SStmIR, SExpIR)
M: 1 C: 73
99%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 16
94%
M: 0 C: 1
100%
splitTarget(SExpIR, List, List)
M: 13 C: 146
92%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 2 C: 29
94%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.vdm2jml.trans;
2:
3: import java.util.LinkedList;
4: import java.util.List;
5:
6: import org.apache.log4j.Logger;
7: import org.overture.codegen.assistant.DeclAssistantIR;
8: import org.overture.codegen.assistant.ExpAssistantIR;
9: import org.overture.codegen.assistant.PatternAssistantIR;
10: import org.overture.codegen.ir.INode;
11: import org.overture.codegen.ir.ITempVarGen;
12: import org.overture.codegen.ir.SExpIR;
13: import org.overture.codegen.ir.SStmIR;
14: import org.overture.codegen.ir.analysis.AnalysisException;
15: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
16: import org.overture.codegen.ir.declarations.AVarDeclIR;
17: import org.overture.codegen.ir.expressions.AFieldExpIR;
18: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
19: import org.overture.codegen.ir.expressions.AMapSeqGetExpIR;
20: import org.overture.codegen.ir.expressions.SVarExpIR;
21: import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
22: import org.overture.codegen.ir.statements.AAssignToExpStmIR;
23: import org.overture.codegen.ir.statements.ABlockStmIR;
24: import org.overture.codegen.ir.statements.AMapSeqUpdateStmIR;
25: import org.overture.codegen.trans.assistants.TransAssistantIR;
26: import org.overture.codegen.vdm2jml.JmlGenerator;
27: import org.overture.codegen.vdm2jml.data.StateDesInfo;
28:
29: public class TargetNormaliserTrans extends DepthFirstAnalysisAdaptor
30: {
31:         public static final String STATE_DES = "stateDes_";
32:
33:         private JmlGenerator jmlGen;
34:
35:         private StateDesInfo stateDesInfo;
36:
37:         private Logger log = Logger.getLogger(this.getClass().getName());
38:
39:         public TargetNormaliserTrans(JmlGenerator jmlGen)
40:         {
41:                 this.jmlGen = jmlGen;
42:                 this.stateDesInfo = new StateDesInfo();
43:         }
44:
45:         @Override
46:         public void caseAFieldExpIR(AFieldExpIR node) throws AnalysisException
47:         {
48:•                if (!(node.parent() instanceof AAssignToExpStmIR))
49:                 {
50:                         return;
51:                 }
52:
53:•                if (!(node.getObject() instanceof SVarExpIR))
54:                 {
55:                         normaliseTarget((AAssignToExpStmIR) node.parent(), node.getObject());
56:                 }
57:         }
58:
59:         @Override
60:         public void caseAMapSeqUpdateStmIR(AMapSeqUpdateStmIR node)
61:                         throws AnalysisException
62:         {
63:•                if (!(node.getCol() instanceof SVarExpIR))
64:                 {
65:                         normaliseTarget(node, node.getCol());
66:                 }
67:         }
68:
69:         private void normaliseTarget(SStmIR node, SExpIR target)
70:         {
71:                 List<AVarDeclIR> varDecls = new LinkedList<AVarDeclIR>();
72:                 List<AIdentifierVarExpIR> vars = new LinkedList<AIdentifierVarExpIR>();
73:
74:                 SExpIR newTarget = splitTarget(target, varDecls, vars);
75:
76:                 markAsCloneFree(varDecls);
77:                 markAsCloneFree(vars);
78:
79:                 stateDesInfo.addStateDesVars(node, vars);
80:                 stateDesInfo.addStateDesDecl(node, varDecls);
81:
82:•                if (varDecls.isEmpty())
83:                 {
84:                         return;
85:                 }
86:
87:                 ABlockStmIR replBlock = new ABlockStmIR();
88:                 jmlGen.getJavaGen().getTransAssistant().replaceNodeWith(node, replBlock);
89:
90:•                for (AVarDeclIR var : varDecls)
91:                 {
92:                         replBlock.getLocalDefs().add(var);
93:                 }
94:
95:                 replBlock.getStatements().add(node);
96:                 jmlGen.getJavaGen().getTransAssistant().replaceNodeWith(target, newTarget);
97:         }
98:
99:         private SExpIR splitTarget(SExpIR target, List<AVarDeclIR> varDecls,
100:                         List<AIdentifierVarExpIR> vars)
101:         {
102:                 DeclAssistantIR dAssist = jmlGen.getJavaGen().getInfo().getDeclAssistant();
103:                 PatternAssistantIR pAssist = jmlGen.getJavaGen().getInfo().getPatternAssistant();
104:                 ExpAssistantIR eAssist = jmlGen.getJavaGen().getInfo().getExpAssistant();
105:                 ITempVarGen nameGen = jmlGen.getJavaGen().getInfo().getTempVarNameGen();
106:                 TransAssistantIR tr = jmlGen.getJavaGen().getTransAssistant();
107:
108:•                if (target instanceof SVarExpIR)
109:                 {
110:                         AIdentifierVarExpIR var = ((AIdentifierVarExpIR) target).clone();
111:                         vars.add(var);
112:                         return var;
113:•                } else if (target instanceof AMapSeqGetExpIR)
114:                 {
115:                         // Utils.mapSeqGet(a.myMap, 1).b
116:                         AMapSeqGetExpIR get = (AMapSeqGetExpIR) target;
117:                         SExpIR newCol = splitTarget(get.getCol().clone(), varDecls, vars);
118:                         tr.replaceNodeWith(get.getCol(), newCol);
119:                         // Utils.mapSeqGet(tmp_1, 1).b
120:
121:                         AIdentifierPatternIR id = pAssist.consIdPattern(nameGen.nextVarName(STATE_DES));
122:                         AVarDeclIR varDecl = dAssist.consLocalVarDecl(get.getType().clone(), id, get.clone());
123:                         varDecls.add(varDecl);
124:                         // B tmp_2 = Utils.mapSeqGet(tmp_1, 1).b
125:
126:                         // tmp_2
127:                         AIdentifierVarExpIR var = eAssist.consIdVar(id.getName(), get.getType().clone());
128:                         vars.add(var);
129:                         return var;
130:
131:•                } else if (target instanceof AFieldExpIR)
132:                 {
133:                         // a.b.c
134:                         AFieldExpIR field = (AFieldExpIR) target;
135:                         SExpIR newObj = splitTarget(field.getObject().clone(), varDecls, vars);
136:                         tr.replaceNodeWith(field.getObject(), newObj);
137:                         // tmp_1.c
138:
139:                         AIdentifierPatternIR id = pAssist.consIdPattern(nameGen.nextVarName(STATE_DES));
140:                         AVarDeclIR varDecl = dAssist.consLocalVarDecl(field.getType().clone(), id, field.clone());
141:                         varDecls.add(varDecl);
142:                         // C tmp_2 = tmp1.c
143:
144:                         AIdentifierVarExpIR var = eAssist.consIdVar(id.getName(), field.getType().clone());
145:                         vars.add(var);
146:                         return var;
147:                 } else
148:                 {
149:                         log.error("Got unexpected target:" + target);
150:                         return null;
151:                 }
152:         }
153:
154:         private void markAsCloneFree(List<? extends INode> nodes)
155:         {
156:•                for (INode v : nodes)
157:                 {
158:                         jmlGen.getJavaGen().getJavaFormat().getValueSemantics().addCloneFreeNode(v);
159:                 }
160:         }
161:
162:         public StateDesInfo getStateDesInfo()
163:         {
164:                 return stateDesInfo;
165:         }
166: }