Package: SlStateAccessTrans

SlStateAccessTrans

nameinstructionbranchcomplexitylinemethod
SlStateAccessTrans(AStateDeclIR, IRInfo, TransAssistantIR)
M: 0 C: 15
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
caseAFuncDeclIR(AFuncDeclIR)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
caseAIdentifierStateDesignatorIR(AIdentifierStateDesignatorIR)
M: 0 C: 81
100%
M: 1 C: 7
88%
M: 1 C: 4
80%
M: 0 C: 17
100%
M: 0 C: 1
100%
caseAIdentifierVarExpIR(AIdentifierVarExpIR)
M: 0 C: 48
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
caseAMethodDeclIR(AMethodDeclIR)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
handleCond(SDeclIR)
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
handleMethodOrFunc(INode, SDeclIR, SDeclIR)
M: 0 C: 12
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
isFieldRead(AStateDeclIR, AIdentifierVarExpIR)
M: 0 C: 28
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
isFieldReadInPreOrPost(AStateDeclIR, AIdentifierVarExpIR)
M: 0 C: 30
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
isOldFieldRead(AStateDeclIR, AIdentifierVarExpIR)
M: 0 C: 24
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setFieldNames(AFieldExpIR, AStateDeclIR, String)
M: 0 C: 51
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 8
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import org.overture.codegen.ir.INode;
4: import org.overture.codegen.ir.IRInfo;
5: import org.overture.codegen.ir.SDeclIR;
6: import org.overture.codegen.ir.analysis.AnalysisException;
7: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
8: import org.overture.codegen.ir.declarations.AFieldDeclIR;
9: import org.overture.codegen.ir.declarations.AFuncDeclIR;
10: import org.overture.codegen.ir.declarations.AMethodDeclIR;
11: import org.overture.codegen.ir.declarations.AStateDeclIR;
12: import org.overture.codegen.ir.expressions.AFieldExpIR;
13: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
14: import org.overture.codegen.ir.statements.AFieldStateDesignatorIR;
15: import org.overture.codegen.ir.statements.AIdentifierStateDesignatorIR;
16: import org.overture.codegen.ir.types.ARecordTypeIR;
17: import org.overture.codegen.trans.assistants.TransAssistantIR;
18:
19: public class SlStateAccessTrans extends DepthFirstAnalysisAdaptor
20: {
21:         private final AStateDeclIR stateDecl;
22:         private IRInfo info;
23:         private TransAssistantIR transAssistant;
24:         private boolean inPreOrPost;
25:
26:         public SlStateAccessTrans(AStateDeclIR stateDecl, IRInfo info,
27:                         TransAssistantIR transAssistant)
28:         {
29:                 this.stateDecl = stateDecl;
30:                 this.info = info;
31:                 this.transAssistant = transAssistant;
32:                 this.inPreOrPost = false;
33:         }
34:
35:         @Override
36:         public void caseAFuncDeclIR(AFuncDeclIR node) throws AnalysisException
37:         {
38:                 handleMethodOrFunc(node.getBody(), node.getPreCond(), node.getPostCond());
39:         }
40:
41:         @Override
42:         public void caseAMethodDeclIR(AMethodDeclIR node) throws AnalysisException
43:         {
44:                 handleMethodOrFunc(node.getBody(), node.getPreCond(), node.getPostCond());
45:         }
46:
47:         @Override
48:         public void caseAIdentifierVarExpIR(AIdentifierVarExpIR node)
49:                         throws AnalysisException
50:         {
51:•                if (isOldFieldRead(stateDecl, node) /* 1 */
52:•                                || isFieldRead(stateDecl, node) /* 2 */
53:•                                || isFieldReadInPreOrPost(stateDecl, node) /* 3 */)
54:                 {
55:                         // Given a state named 'St' with a field named 'f'
56:                         // Note: If a variable is 'old' then it is local since state is passed
57:                         // as an argument to the post condition function
58:
59:                         // Also note that the IR uses underscore to denote old names: _St corresponds to St~ (in VDM)
60:                         // and _f corresponds to f~ (in VDM)
61:
62:                         // /*1*/ In case we are in a post condition the variable expression may represent a
63:                         // field of the old state, i.e._f (or f~ in VDM)
64:                         //
65:                         // /*2*/ Any other place in the model:
66:                         // The variable expression represents a field of the current state
67:                         //
68:                         // /*3*/ Another possibility is that the variable expression represents
69:                         // a field of the current state in a pre or post condition of a function or
70:                         // an operation. This is a special case since the state field can be
71:                         // read as 'f' although it is only the entire state that is being passed as an argument
72:                         // to the function. Below is a Java example showing this (result, old state,
73:                         // current state):
74:                         //
75:                         // public static Boolean post_op(final Number RESULT, final St _St, final St St)
76:                         // If /*1*/ or /*2*/ or /*3*/ is true we make the field access explicit since IR modules
77:                         // use a single field to represent state.
78:                         //
79:                         AFieldExpIR fieldExp = new AFieldExpIR();
80:                         fieldExp.setSourceNode(node.getSourceNode());
81:                         fieldExp.setTag(node.getTag());
82:                         fieldExp.setType(node.getType().clone());
83:                         setFieldNames(fieldExp, stateDecl, node.getName());
84:
85:                         transAssistant.replaceNodeWith(node, fieldExp);
86:                 }
87:         }
88:
89:         @Override
90:         public void caseAIdentifierStateDesignatorIR(
91:                         AIdentifierStateDesignatorIR node) throws AnalysisException
92:         {
93:•                if (!node.getIsLocal() && !node.getName().equals(stateDecl.getName()))
94:                 {
95:                         ARecordTypeIR stateType = transAssistant.getRecType(stateDecl);
96:
97:                         AIdentifierStateDesignatorIR idState = new AIdentifierStateDesignatorIR();
98:                         idState.setClassName(null);
99:                         idState.setExplicit(false);
100:                         idState.setIsLocal(false);
101:                         idState.setName(stateDecl.getName());
102:                         idState.setType(stateType);
103:
104:                         AFieldStateDesignatorIR field = new AFieldStateDesignatorIR();
105:                         field.setField(node.getName());
106:                         field.setObject(idState);
107:•                        for (AFieldDeclIR f : stateDecl.getFields())
108:                         {
109:•                                if (f.getName().equals(node.getName()))
110:                                 {
111:                                         field.setType(f.getType().clone());
112:                                 }
113:                         }
114:
115:                         transAssistant.replaceNodeWith(node, field);
116:                 }
117:         }
118:
119:         private void handleMethodOrFunc(INode body, SDeclIR preCond,
120:                         SDeclIR postCond) throws AnalysisException
121:         {
122:•                if (body != null)
123:                 {
124:                         body.apply(this);
125:                 }
126:
127:                 handleCond(preCond);
128:                 handleCond(postCond);
129:         }
130:
131:         private void handleCond(SDeclIR cond) throws AnalysisException
132:         {
133:•                if (cond != null)
134:                 {
135:                         inPreOrPost = true;
136:                         cond.apply(this);
137:                         inPreOrPost = false;
138:                 }
139:         }
140:
141:         private boolean isFieldReadInPreOrPost(final AStateDeclIR stateDecl,
142:                         AIdentifierVarExpIR node)
143:         {
144:•                if (!inPreOrPost)
145:                 {
146:                         return false;
147:                 }
148:
149:                 boolean matches = false;
150:•                for (AFieldDeclIR f : stateDecl.getFields())
151:                 {
152:•                        if (f.getName().equals(node.getName()))
153:                         {
154:                                 matches = true;
155:                                 break;
156:                         }
157:                 }
158:
159:                 return matches;
160:         }
161:
162:         private boolean isFieldRead(final AStateDeclIR stateDecl, AIdentifierVarExpIR node) {
163:•                return !info.isSlStateRead(node) && !node.getName().equals(stateDecl.getName())
164:•                                && !node.getName().equals("_" + stateDecl.getName());
165:         }
166:
167:         private boolean isOldFieldRead(final AStateDeclIR stateDecl,
168:                         AIdentifierVarExpIR node)
169:         {
170:•                return info.getExpAssistant().isOld(node.getName())
171:•                                && !node.getName().equals("_" + stateDecl.getName());
172:         }
173:
174:         private void setFieldNames(AFieldExpIR field, AStateDeclIR stateDecl,
175:                         String name)
176:         {
177:                 ARecordTypeIR recType = transAssistant.getRecType(stateDecl);
178:                 String stateName = stateDecl.getName();
179:
180:•                if (info.getExpAssistant().isOld(name))
181:                 {
182:                         field.setObject(transAssistant.getInfo().getExpAssistant().consIdVar("_"
183:                                         + stateName, recType));
184:                         field.setMemberName(info.getExpAssistant().oldNameToCurrentName(name));
185:                 } else
186:                 {
187:                         field.setObject(transAssistant.getInfo().getExpAssistant().consIdVar(stateName, recType));
188:                         field.setMemberName(name);
189:                 }
190:         }
191: }