Package: PostCheckTrans

PostCheckTrans

nameinstructionbranchcomplexitylinemethod
PostCheckTrans(IPostCheckCreator, TransAssistantIR, String, Object)
M: 0 C: 21
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
caseAMethodDeclIR(AMethodDeclIR)
M: 23 C: 34
60%
M: 4 C: 8
67%
M: 4 C: 3
43%
M: 7 C: 13
65%
M: 0 C: 1
100%
caseAReturnStmIR(AReturnStmIR)
M: 22 C: 117
84%
M: 4 C: 6
60%
M: 4 C: 2
33%
M: 6 C: 22
79%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import org.apache.log4j.Logger;
4: import org.overture.ast.definitions.AExplicitFunctionDefinition;
5: import org.overture.ast.node.INode;
6: import org.overture.codegen.ir.SDeclIR;
7: import org.overture.codegen.ir.SExpIR;
8: import org.overture.codegen.ir.SourceNode;
9: import org.overture.codegen.ir.analysis.AnalysisException;
10: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
11: import org.overture.codegen.ir.declarations.AMethodDeclIR;
12: import org.overture.codegen.ir.declarations.AVarDeclIR;
13: import org.overture.codegen.ir.expressions.AApplyExpIR;
14: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
15: import org.overture.codegen.ir.expressions.AStringLiteralExpIR;
16: import org.overture.codegen.ir.statements.ABlockStmIR;
17: import org.overture.codegen.ir.statements.AReturnStmIR;
18: import org.overture.codegen.trans.assistants.TransAssistantIR;
19:
20: public class PostCheckTrans extends DepthFirstAnalysisAdaptor
21: {
22:         private IPostCheckCreator postCheckCreator;
23:         private TransAssistantIR transAssistant;
24:         private String funcResultNamePrefix;
25:         private Object conditionalCallTag;
26:
27:         private Logger log = Logger.getLogger(this.getClass().getName());
28:
29:         public PostCheckTrans(IPostCheckCreator postCheckCreator,
30:                         TransAssistantIR transAssistant, String funcResultNamePrefix,
31:                         Object conditionalCallTag)
32:         {
33:                 this.postCheckCreator = postCheckCreator;
34:                 this.transAssistant = transAssistant;
35:                 this.funcResultNamePrefix = funcResultNamePrefix;
36:                 this.conditionalCallTag = conditionalCallTag;
37:         }
38:
39:         @Override
40:         public void caseAMethodDeclIR(AMethodDeclIR node) throws AnalysisException
41:         {
42:•                if (!transAssistant.getInfo().getSettings().generatePostCondChecks())
43:                 {
44:                         return;
45:                 }
46:
47:                 SDeclIR postCond = node.getPostCond();
48:
49:•                if (postCond == null)
50:                 {
51:                         return;
52:                 }
53:
54:•                if (!(postCond instanceof AMethodDeclIR))
55:                 {
56:                         log.error("Expected post condition to be a method declaration at this point. Got: "
57:                                         + postCond);
58:                         return;
59:                 }
60:
61:                 SourceNode sourceNode = postCond.getSourceNode();
62:
63:•                if (sourceNode == null)
64:                 {
65:                         log.error("Could not find source node for method declaration");
66:                         return;
67:                 }
68:
69:                 INode vdmNode = sourceNode.getVdmNode();
70:
71:•                if (vdmNode == null)
72:                 {
73:                         log.error("Could not find VDM source node for method declaration");
74:                         return;
75:                 }
76:
77:•                if (!(vdmNode instanceof AExplicitFunctionDefinition))
78:                 {
79:                         // Generation of post conditions is not supported for operations
80:                         return;
81:                 }
82:
83:                 node.getBody().apply(this);
84:         }
85:
86:         @Override
87:         public void caseAReturnStmIR(AReturnStmIR node) throws AnalysisException
88:         {
89:                 SExpIR result = node.getExp();
90:
91:•                if (result == null)
92:                 {
93:                         log.error("Expected a value to be returned");
94:                         return;
95:                 }
96:
97:                 AMethodDeclIR method = node.getAncestor(AMethodDeclIR.class);
98:
99:•                if (method == null)
100:                 {
101:                         log.error("Could not find enclosing method for a return statement");
102:                         return;
103:                 }
104:
105:•                if (method.getStatic() == null || !method.getStatic())
106:                 {
107:                         // Generation of a post condition is only supported for static operations
108:                         // where no 'self' and '~self' are being passed
109:                         return;
110:                 }
111:
112:                 SDeclIR postCond = method.getPostCond();
113:
114:•                if (!(postCond instanceof AMethodDeclIR))
115:                 {
116:                         log.error("Expected post condition to be a method declaration at this point. Got: "
117:                                         + postCond);
118:                         return;
119:                 }
120:
121:                 AApplyExpIR postCondCall = transAssistant.consConditionalCall(method, (AMethodDeclIR) method.getPostCond());
122:                 postCondCall.setTag(conditionalCallTag);
123:
124:                 String funcResultVarName = transAssistant.getInfo().getTempVarNameGen().nextVarName(funcResultNamePrefix);
125:                 AVarDeclIR resultDecl = transAssistant.consDecl(funcResultVarName, method.getMethodType().getResult().clone(), node.getExp().clone());
126:                 AIdentifierVarExpIR resultVar = transAssistant.getInfo().getExpAssistant().consIdVar(funcResultVarName, resultDecl.getType().clone());
127:
128:                 postCondCall.getArgs().add(resultVar.clone());
129:                 AStringLiteralExpIR methodName = transAssistant.getInfo().getExpAssistant().consStringLiteral(method.getName(), false);
130:                 AApplyExpIR postCheckCall = postCheckCreator.consPostCheckCall(method, postCondCall, resultVar, methodName);
131:
132:                 ABlockStmIR replacementBlock = new ABlockStmIR();
133:                 replacementBlock.getLocalDefs().add(resultDecl);
134:
135:                 transAssistant.replaceNodeWith(node.getExp(), postCheckCall);
136:                 transAssistant.replaceNodeWith(node, replacementBlock);
137:
138:                 replacementBlock.getStatements().add(node);
139:         }
140: }