Package: AtomicStmTrans

AtomicStmTrans

nameinstructionbranchcomplexitylinemethod
AtomicStmTrans(TransAssistantIR, String)
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%
caseAAtomicStmIR(AAtomicStmIR)
M: 11 C: 87
89%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 17
94%
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.SExpIR;
5: import org.overture.codegen.ir.SStmIR;
6: import org.overture.codegen.ir.STypeIR;
7: import org.overture.codegen.ir.analysis.AnalysisException;
8: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
9: import org.overture.codegen.ir.declarations.AVarDeclIR;
10: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
11: import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
12: import org.overture.codegen.ir.statements.AAssignmentStmIR;
13: import org.overture.codegen.ir.statements.AAtomicStmIR;
14: import org.overture.codegen.ir.statements.ABlockStmIR;
15: import org.overture.codegen.trans.assistants.TransAssistantIR;
16:
17: public class AtomicStmTrans extends DepthFirstAnalysisAdaptor
18: {
19:         // In VDM...
20:         // atomic
21:         // (
22:         // sd1 := ec1;
23:         // ...;
24:         // sdN := ecN
25:         // )
26:         //
27:         // ..Is evaluated as:
28:         //
29:         // let t1 : T1 = ec1,
30:         // ...
31:         // tN : TN = ecN in
32:         // (
33:         // -- turn off invariants, threading and durations
34:         // sd1 := t1;
35:         // ...
36:         // sdN := tN;
37:         // -- turn on invariants, threading and durations
38:         // -- and check that invariants hold.
39:         // );
40:
41:         private TransAssistantIR transAssistant;
42:         private String atomicPrefix;
43:
44:         private Logger log = Logger.getLogger(this.getClass().getName());
45:
46:         public AtomicStmTrans(TransAssistantIR transAssistant, String atomicPrefix)
47:         {
48:                 this.transAssistant = transAssistant;
49:                 this.atomicPrefix = atomicPrefix;
50:         }
51:
52:         @Override
53:         public void caseAAtomicStmIR(AAtomicStmIR node) throws AnalysisException
54:         {
55:                 ABlockStmIR tmpBlock = new ABlockStmIR();
56:
57:•                for (SStmIR stm : node.getStatements())
58:                 {
59:•                        if (stm instanceof AAssignmentStmIR)
60:                         {
61:                                 AAssignmentStmIR assign = (AAssignmentStmIR) stm;
62:
63:                                 // Build temporary variable
64:                                 String name = transAssistant.getInfo().getTempVarNameGen().nextVarName(atomicPrefix);
65:                                 STypeIR type = assign.getTarget().getType().clone();
66:                                 AIdentifierPatternIR pattern = transAssistant.getInfo().getPatternAssistant().consIdPattern(name);
67:                                 SExpIR exp = assign.getExp().clone();
68:
69:                                 AVarDeclIR varDecl = transAssistant.getInfo().getDeclAssistant().consLocalVarDecl(type, pattern, exp);
70:
71:                                 // Add temporary variable to the block
72:                                 tmpBlock.getLocalDefs().add(varDecl);
73:
74:                                 // Assign state designator to temporary variable
75:                                 AIdentifierVarExpIR tmpVarExp = transAssistant.getInfo().getExpAssistant().consIdVar(name, type.clone());
76:                                 transAssistant.replaceNodeWith(assign.getExp(), tmpVarExp);
77:                         } else
78:                         {
79:                                 log.error("Expected all statements in atomic statement block to be assignments. Got: "
80:                                                 + stm);
81:                         }
82:                 }
83:
84:                 // Replace the atomic statement with the 'let' statement and make
85:                 // the atomic statement its body
86:                 transAssistant.replaceNodeWith(node, tmpBlock);
87:                 tmpBlock.getStatements().add(node);
88:         }
89: }