Package: WhileStmTrans

WhileStmTrans

nameinstructionbranchcomplexitylinemethod
WhileStmTrans(TransAssistantIR, String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
caseAWhileStmIR(AWhileStmIR)
M: 0 C: 101
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 21
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import org.overture.codegen.ir.SExpIR;
4: import org.overture.codegen.ir.SStmIR;
5: import org.overture.codegen.ir.analysis.AnalysisException;
6: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
7: import org.overture.codegen.ir.declarations.AVarDeclIR;
8: import org.overture.codegen.ir.statements.ABlockStmIR;
9: import org.overture.codegen.ir.statements.ABreakStmIR;
10: import org.overture.codegen.ir.statements.AIfStmIR;
11: import org.overture.codegen.ir.statements.AWhileStmIR;
12: import org.overture.codegen.trans.assistants.TransAssistantIR;
13:
14: public class WhileStmTrans extends DepthFirstAnalysisAdaptor
15: {
16:         private TransAssistantIR transAssistant;
17:         private String whileCondExpPrefix;
18:
19:         public WhileStmTrans(TransAssistantIR transAssistant,
20:                         String whileCondExpPrefix)
21:         {
22:                 this.transAssistant = transAssistant;
23:                 this.whileCondExpPrefix = whileCondExpPrefix;
24:         }
25:
26:         @Override
27:         public void caseAWhileStmIR(AWhileStmIR node) throws AnalysisException
28:         {
29:                 // while(boolExp) { body; }
30:                 //
31:                 // boolExp is replaced with a variable expression 'whileCond' that is
32:                 // computed as set for each iteration in the while loop:
33:                 //
34:                 // boolean whileCond = true;
35:                 //
36:                 // while(whileCond)
37:                 // {
38:                 // whileCond = boolExp;
39:                 // if (!whileCond) { break; }
40:                 // body;
41:                 // }
42:                 //
43:                 // This is needed for cases where the while condition is a complex
44:                 // expression that needs to be transformed. For example, when the
45:                 // while condition is a quantified expression
46:
47:                 SExpIR exp = node.getExp().clone();
48:                 SStmIR body = node.getBody().clone();
49:
50:                 String whileCondName = transAssistant.getInfo().getTempVarNameGen().nextVarName(whileCondExpPrefix);
51:
52:                 SExpIR whileCondVar = transAssistant.consBoolCheck(whileCondName, false);
53:
54:                 AIfStmIR whileCondCheck = new AIfStmIR();
55:                 whileCondCheck.setIfExp(transAssistant.consBoolCheck(whileCondName, true));
56:                 whileCondCheck.setThenStm(new ABreakStmIR());
57:
58:                 ABlockStmIR newWhileBody = new ABlockStmIR();
59:                 newWhileBody.getStatements().add(transAssistant.consBoolVarAssignment(exp, whileCondName));
60:                 newWhileBody.getStatements().add(whileCondCheck);
61:                 newWhileBody.getStatements().add(body);
62:
63:                 AWhileStmIR newWhileStm = new AWhileStmIR();
64:                 newWhileStm.setExp(whileCondVar);
65:                 newWhileStm.setBody(newWhileBody);
66:
67:                 ABlockStmIR declBlock = new ABlockStmIR();
68:                 AVarDeclIR whileCondVarDecl = transAssistant.consBoolVarDecl(whileCondName, true);
69:                 declBlock.getLocalDefs().add(whileCondVarDecl);
70:                 declBlock.getStatements().add(newWhileStm);
71:
72:                 transAssistant.replaceNodeWith(node, declBlock);
73:
74:                 newWhileStm.getBody().apply(this);
75:         }
76: }