Package: TracesTrans

TracesTrans

nameinstructionbranchcomplexitylinemethod
TracesTrans(TransAssistantIR, IterationVarPrefixes, TraceNames, ILanguageIterator, ICallStmToStringMethodBuilder, List)
M: 0 C: 27
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
buildTestExecutionStms(AIdentifierVarExpIR, String)
M: 0 C: 88
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 14
100%
M: 0 C: 1
100%
caseANamedTraceDeclIR(ANamedTraceDeclIR)
M: 13 C: 31
70%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 4 C: 7
64%
M: 0 C: 1
100%
consStmBuilder(StoreAssistant, String)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
consTraceMethod(ANamedTraceDeclIR)
M: 0 C: 103
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
consTraceMethodBody(ANamedTraceDeclIR)
M: 0 C: 83
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 13
100%
M: 0 C: 1
100%
getClassName(ANamedTraceDeclIR)
M: 13 C: 12
48%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 4
67%
M: 0 C: 1
100%
getCloneFreeNodes()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getIteVarPrefixes()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getLangIterator()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getToStringBuilder()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTraceName(ANamedTraceDeclIR)
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getTracePrefixes()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTransAssist()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
registerOtherModules(String, StoreAssistant)
M: 0 C: 84
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
traceIsSupported(TraceSupportedAnalysis)
M: 10 C: 9
47%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 4 C: 3
43%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.traces;
2:
3: import java.util.List;
4:
5: import org.apache.log4j.Logger;
6: import org.overture.ast.lex.Dialect;
7: import org.overture.codegen.ir.INode;
8: import org.overture.codegen.ir.IRConstants;
9: import org.overture.codegen.ir.SStmIR;
10: import org.overture.codegen.ir.analysis.AnalysisException;
11: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
12: import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
13: import org.overture.codegen.ir.declarations.AFormalParamLocalParamIR;
14: import org.overture.codegen.ir.declarations.AMethodDeclIR;
15: import org.overture.codegen.ir.declarations.ANamedTraceDeclIR;
16: import org.overture.codegen.ir.declarations.SClassDeclIR;
17: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
18: import org.overture.codegen.ir.expressions.ATypeArgExpIR;
19: import org.overture.codegen.ir.statements.ABlockStmIR;
20: import org.overture.codegen.ir.statements.APlainCallStmIR;
21: import org.overture.codegen.ir.types.AClassTypeIR;
22: import org.overture.codegen.ir.types.AExternalTypeIR;
23: import org.overture.codegen.ir.types.AMethodTypeIR;
24: import org.overture.codegen.ir.types.AVoidTypeIR;
25: import org.overture.codegen.trans.IterationVarPrefixes;
26: import org.overture.codegen.trans.assistants.TransAssistantIR;
27: import org.overture.codegen.trans.iterator.ILanguageIterator;
28: import org.overture.config.Settings;
29:
30: public class TracesTrans extends DepthFirstAnalysisAdaptor
31: {
32:         protected TransAssistantIR transAssistant;
33:         protected IterationVarPrefixes iteVarPrefixes;
34:         protected ILanguageIterator langIterator;
35:         protected ICallStmToStringMethodBuilder toStringBuilder;
36:         protected TraceNames tracePrefixes;
37:         private List<INode> cloneFreeNodes;
38:
39:         private Logger log = Logger.getLogger(this.getClass().getName());
40:
41:         public TracesTrans(TransAssistantIR transAssistant,
42:                         IterationVarPrefixes iteVarPrefixes, TraceNames tracePrefixes,
43:                         ILanguageIterator langIterator,
44:                         ICallStmToStringMethodBuilder toStringBuilder,
45:                         List<INode> cloneFreeNodes)
46:         {
47:                 this.transAssistant = transAssistant;
48:                 this.iteVarPrefixes = iteVarPrefixes;
49:                 this.langIterator = langIterator;
50:                 this.toStringBuilder = toStringBuilder;
51:
52:                 this.tracePrefixes = tracePrefixes;
53:                 this.cloneFreeNodes = cloneFreeNodes;
54:         }
55:
56:         @Override
57:         public void caseANamedTraceDeclIR(ANamedTraceDeclIR node)
58:                         throws AnalysisException
59:         {
60:•                if (!transAssistant.getInfo().getSettings().generateTraces())
61:                 {
62:                         return;
63:                 }
64:
65:                 TraceSupportedAnalysis supportedAnalysis = new TraceSupportedAnalysis(node);
66:•                if (!traceIsSupported(supportedAnalysis))
67:                 {
68:                         transAssistant.getInfo().addTransformationWarning(node, supportedAnalysis.getReason());
69:                         return;
70:                 }
71:
72:                 ADefaultClassDeclIR enclosingClass = node.getAncestor(ADefaultClassDeclIR.class);
73:
74:•                if (enclosingClass != null)
75:                 {
76:                         enclosingClass.getMethods().add(consTraceMethod(node));
77:                 } else
78:                 {
79:                         log.error("Class enclosing trace could not be found so the generated trace could not be added as a method to the corresponding class");
80:                 }
81:         }
82:
83:         private boolean traceIsSupported(TraceSupportedAnalysis supportedAnalysis)
84:         {
85:                 try
86:                 {
87:                         supportedAnalysis.run();
88:                 } catch (AnalysisException e)
89:                 {
90:                         log.error("Could not determine if a trace could be code generated");
91:                         e.printStackTrace();
92:                         return false;
93:                 }
94:
95:•                return !supportedAnalysis.isUnsupported();
96:         }
97:
98:         private AMethodDeclIR consTraceMethod(ANamedTraceDeclIR node)
99:                         throws AnalysisException
100:         {
101:                 AClassTypeIR testAccType = transAssistant.consClassType(tracePrefixes.testAccumulatorClassName());
102:
103:                 AMethodTypeIR methodType = new AMethodTypeIR();
104:                 methodType.setResult(new AVoidTypeIR());
105:                 methodType.getParams().add(testAccType);
106:
107:                 AFormalParamLocalParamIR instanceParam = new AFormalParamLocalParamIR();
108:                 instanceParam.setType(testAccType.clone());
109:                 instanceParam.setPattern(transAssistant.getInfo().getPatternAssistant().consIdPattern(tracePrefixes.traceMethodParamName()));
110:
111:                 AMethodDeclIR traceMethod = new AMethodDeclIR();
112:                 traceMethod.setTag(new TraceMethodTag());
113:
114:                 traceMethod.getFormalParams().add(instanceParam);
115:
116:                 traceMethod.setImplicit(false);
117:                 traceMethod.setAbstract(false);
118:                 traceMethod.setAccess(IRConstants.PUBLIC);
119:                 traceMethod.setBody(consTraceMethodBody(node));
120:                 traceMethod.setIsConstructor(false);
121:•                traceMethod.setStatic(Settings.dialect == Dialect.VDM_SL);
122:                 traceMethod.setMethodType(methodType);
123:                 traceMethod.setName(getTraceName(node) + "_"
124:                                 + tracePrefixes.runTraceMethodName());
125:
126:                 return traceMethod;
127:         }
128:
129:         private SStmIR buildTestExecutionStms(AIdentifierVarExpIR nodeVar,
130:                         String traceEnclosingClassName)
131:         {
132:                 AExternalTypeIR utilsType = new AExternalTypeIR();
133:                 utilsType.setName(tracePrefixes.traceNodeNodeClassName());
134:
135:                 APlainCallStmIR executeTestsCall = new APlainCallStmIR();
136:                 executeTestsCall.setClassType(utilsType);
137:                 executeTestsCall.setName(tracePrefixes.executeTestsMethodName());
138:                 executeTestsCall.setType(new AVoidTypeIR());
139:
140:                 ATypeArgExpIR typeArg = new ATypeArgExpIR();
141:                 typeArg.setType(transAssistant.consClassType(traceEnclosingClassName));
142:
143:                 executeTestsCall.getArgs().add(nodeVar.clone());
144:
145:•                if (Settings.dialect != Dialect.VDM_SL)
146:                 {
147:                         executeTestsCall.getArgs().add(typeArg);
148:                 }
149:
150:                 executeTestsCall.getArgs().add(transAssistant.getInfo().getExpAssistant().consIdVar(tracePrefixes.traceMethodParamName(), transAssistant.consClassType(tracePrefixes.testAccumulatorClassName())));
151:                 executeTestsCall.getArgs().add(transAssistant.getInfo().getExpAssistant().consIdVar(tracePrefixes.storeVarName(), transAssistant.consClassType(tracePrefixes.storeClassName())));
152:
153:                 return executeTestsCall;
154:         }
155:
156:         private SStmIR consTraceMethodBody(ANamedTraceDeclIR node)
157:                         throws AnalysisException
158:         {
159:                 StoreAssistant storeAssist = new StoreAssistant(tracePrefixes, transAssistant);
160:
161:                 String traceEnclosingClass = getClassName(node);
162:
163:                 TraceStmBuilder stmBuilder = consStmBuilder(storeAssist, traceEnclosingClass);
164:
165:                 SStmIR regModules = registerOtherModules(traceEnclosingClass, storeAssist);
166:                 TraceNodeData nodeData = stmBuilder.buildFromDeclTerms(node.getTerms());
167:
168:                 ABlockStmIR stms = new ABlockStmIR();
169:                 stms.getLocalDefs().add(transAssistant.consClassVarDeclDefaultCtor(tracePrefixes.storeClassName(), tracePrefixes.storeVarName()));
170:                 stms.getLocalDefs().add(transAssistant.consClassVarDeclDefaultCtor(tracePrefixes.idGeneratorClassName(), tracePrefixes.idGeneratorVarName()));
171:
172:•                if (regModules != null)
173:                 {
174:                         stms.getStatements().add(regModules);
175:                 }
176:
177:                 stms.getStatements().add(nodeData.getStms());
178:                 stms.getStatements().add(buildTestExecutionStms(nodeData.getNodeVar(), getClassName(node)));
179:
180:                 return stms;
181:         }
182:
183:         public TraceStmBuilder consStmBuilder(StoreAssistant storeAssist,
184:                         String traceEnclosingClass)
185:         {
186:                 return new TraceStmBuilder(this, traceEnclosingClass, storeAssist);
187:         }
188:
189:         private SStmIR registerOtherModules(String encClass,
190:                         StoreAssistant storeAssist)
191:         {
192:                 // Static registration of other modules
193:•                if (Settings.dialect == Dialect.VDM_PP
194:•                                && transAssistant.getInfo().getClasses().size() == 1)
195:                 {
196:                         return null;
197:                 }
198:
199:                 ABlockStmIR regBlock = new ABlockStmIR();
200:                 regBlock.setScoped(true);
201:
202:•                for (SClassDeclIR c : transAssistant.getInfo().getClasses())
203:                 {
204:•                        if (Settings.dialect == Dialect.VDM_PP
205:•                                        && c.getName().equals(encClass))
206:                         {
207:                                 /**
208:                                  * There is no need to register the instance of the enclosing class. This is handled by the
209:                                  * TraceNode.executeTests method
210:                                  */
211:                                 continue;
212:                         }
213:
214:                         String idConstName = transAssistant.getInfo().getTempVarNameGen().nextVarName(tracePrefixes.idConstNamePrefix());
215:                         regBlock.getLocalDefs().add(storeAssist.consIdConstDecl(idConstName));
216:
217:                         AClassTypeIR classType = transAssistant.consClassType(c.getName());
218:
219:                         ATypeArgExpIR classArg = new ATypeArgExpIR();
220:                         classArg.setType(classType.clone());
221:
222:                         regBlock.getStatements().add(storeAssist.consStoreRegistration(idConstName, classArg, true));
223:                 }
224:
225:                 return regBlock;
226:         }
227:
228:         private String getTraceName(ANamedTraceDeclIR node)
229:         {
230:                 String methodName = getClassName(node);
231:
232:•                for (int i = 0; i < node.getPathname().size(); i++)
233:                 {
234:                         methodName += "_" + node.getPathname().get(i).getName();
235:                 }
236:
237:                 return methodName;
238:         }
239:
240:         public String getClassName(ANamedTraceDeclIR trace)
241:         {
242:•                if (trace != null)
243:                 {
244:                         ADefaultClassDeclIR enclosingClass = trace.getAncestor(ADefaultClassDeclIR.class);
245:•                        if (enclosingClass != null)
246:                         {
247:                                 return enclosingClass.getName();
248:                         }
249:                 }
250:
251:                 log.error("Could not find class declaration enclosing the trace node "
252:                                 + trace);
253:
254:                 return null;
255:         }
256:
257:         public TransAssistantIR getTransAssist()
258:         {
259:                 return transAssistant;
260:         }
261:
262:         public IterationVarPrefixes getIteVarPrefixes()
263:         {
264:                 return iteVarPrefixes;
265:         }
266:
267:         public TraceNames getTracePrefixes()
268:         {
269:                 return tracePrefixes;
270:         }
271:
272:         public ILanguageIterator getLangIterator()
273:         {
274:                 return langIterator;
275:         }
276:
277:         public ICallStmToStringMethodBuilder getToStringBuilder()
278:         {
279:                 return toStringBuilder;
280:         }
281:
282:         public List<INode> getCloneFreeNodes()
283:         {
284:                 return cloneFreeNodes;
285:         }
286: }