Package: PreCheckTrans

PreCheckTrans

nameinstructionbranchcomplexitylinemethod
PreCheckTrans(TransAssistantIR, Object)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
caseAMethodDeclIR(AMethodDeclIR)
M: 13 C: 97
88%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 3 C: 24
89%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import org.apache.log4j.Logger;
4: import org.overture.codegen.ir.SDeclIR;
5: import org.overture.codegen.ir.SStmIR;
6: import org.overture.codegen.ir.analysis.AnalysisException;
7: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
8: import org.overture.codegen.ir.declarations.AMethodDeclIR;
9: import org.overture.codegen.ir.expressions.AApplyExpIR;
10: import org.overture.codegen.ir.expressions.APreCondRuntimeErrorExpIR;
11: import org.overture.codegen.ir.statements.ABlockStmIR;
12: import org.overture.codegen.ir.statements.AIfStmIR;
13: import org.overture.codegen.ir.statements.ARaiseErrorStmIR;
14: import org.overture.codegen.ir.types.AErrorTypeIR;
15: import org.overture.codegen.trans.assistants.TransAssistantIR;
16:
17: public class PreCheckTrans extends DepthFirstAnalysisAdaptor
18: {
19:
20:         private TransAssistantIR transAssistant;
21:         private Object conditionalCallTag;
22:
23:         private Logger log = Logger.getLogger(this.getClass().getName());
24:
25:         public PreCheckTrans(TransAssistantIR transAssistant,
26:                         Object conditionalCallTag)
27:         {
28:                 this.transAssistant = transAssistant;
29:                 this.conditionalCallTag = conditionalCallTag;
30:         }
31:
32:         @Override
33:         public void caseAMethodDeclIR(AMethodDeclIR node) throws AnalysisException
34:         {
35:
36:•                if (!transAssistant.getInfo().getSettings().generatePreCondChecks())
37:                 {
38:                         return;
39:                 }
40:
41:                 SDeclIR preCond = node.getPreCond();
42:
43:•                if (preCond == null)
44:                 {
45:                         return;
46:                 }
47:
48:•                if (!(preCond instanceof AMethodDeclIR))
49:                 {
50:                         log.error("Expected pre condition to be a method declaration at this point. Got: "
51:                                         + preCond);
52:                         return;
53:                 }
54:
55:                 AMethodDeclIR preCondMethod = (AMethodDeclIR) preCond;
56:
57:                 AApplyExpIR preCondCall = transAssistant.consConditionalCall(node, preCondMethod);
58:
59:•                if (preCondCall == null)
60:                 {
61:                         return;
62:                 }
63:
64:                 preCondCall.setTag(conditionalCallTag);
65:
66:                 SStmIR body = node.getBody();
67:
68:                 APreCondRuntimeErrorExpIR runtimeError = new APreCondRuntimeErrorExpIR();
69:                 runtimeError.setType(new AErrorTypeIR());
70:                 runtimeError.setMessage(String.format("Precondition failure: pre_%s", node.getName()));
71:
72:                 ARaiseErrorStmIR raiseError = new ARaiseErrorStmIR();
73:                 raiseError.setError(runtimeError);
74:
75:                 AIfStmIR ifCheck = new AIfStmIR();
76:                 ifCheck.setIfExp(transAssistant.getInfo().getExpAssistant().negate(preCondCall));
77:                 ifCheck.setThenStm(raiseError);
78:
79:                 ABlockStmIR newBody = new ABlockStmIR();
80:                 newBody.getStatements().add(ifCheck);
81:                 newBody.getStatements().add(body.clone());
82:
83:                 transAssistant.replaceNodeWith(body, newBody);
84:         }
85: }