Package: ModuleToClassTransformation

ModuleToClassTransformation

nameinstructionbranchcomplexitylinemethod
ModuleToClassTransformation(IRInfo, TransAssistantIR, List)
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%
caseAModuleDeclIR(AModuleDeclIR)
M: 11 C: 265
96%
M: 1 C: 21
95%
M: 1 C: 11
92%
M: 1 C: 59
98%
M: 0 C: 1
100%
getInitExp(AStateDeclIR)
M: 0 C: 48
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
getResult()
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%
getStateDecl(AModuleDeclIR)
M: 0 C: 20
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
getValue(String, String)
M: 59 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
handleImports(AModuleImportsIR, ADefaultClassDeclIR)
M: 0 C: 45
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
makeStateAccessExplicit(AModuleDeclIR)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import java.util.LinkedList;
4: import java.util.List;
5:
6: import org.apache.log4j.Logger;
7: import org.overture.codegen.ir.IRConstants;
8: import org.overture.codegen.ir.IRInfo;
9: import org.overture.codegen.ir.PIR;
10: import org.overture.codegen.ir.SDeclIR;
11: import org.overture.codegen.ir.SExpIR;
12: import org.overture.codegen.ir.SImportIR;
13: import org.overture.codegen.ir.analysis.AnalysisException;
14: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
15: import org.overture.codegen.ir.declarations.AAllImportIR;
16: import org.overture.codegen.ir.declarations.ADefaultClassDeclIR;
17: import org.overture.codegen.ir.declarations.AFieldDeclIR;
18: import org.overture.codegen.ir.declarations.AFromModuleImportsIR;
19: import org.overture.codegen.ir.declarations.AFuncDeclIR;
20: import org.overture.codegen.ir.declarations.AFunctionValueImportIR;
21: import org.overture.codegen.ir.declarations.AMethodDeclIR;
22: import org.overture.codegen.ir.declarations.AModuleDeclIR;
23: import org.overture.codegen.ir.declarations.AModuleImportsIR;
24: import org.overture.codegen.ir.declarations.ANamedTraceDeclIR;
25: import org.overture.codegen.ir.declarations.AOperationValueImportIR;
26: import org.overture.codegen.ir.declarations.ARecordDeclIR;
27: import org.overture.codegen.ir.declarations.AStateDeclIR;
28: import org.overture.codegen.ir.declarations.ATypeDeclIR;
29: import org.overture.codegen.ir.declarations.ATypeImportIR;
30: import org.overture.codegen.ir.declarations.AValueValueImportIR;
31: import org.overture.codegen.ir.expressions.AEqualsBinaryExpIR;
32: import org.overture.codegen.ir.expressions.ANewExpIR;
33: import org.overture.codegen.ir.expressions.AUndefinedExpIR;
34: import org.overture.codegen.ir.name.ATypeNameIR;
35: import org.overture.codegen.ir.types.ARecordTypeIR;
36: import org.overture.codegen.trans.assistants.TransAssistantIR;
37:
38: public class ModuleToClassTransformation extends DepthFirstAnalysisAdaptor
39:                 implements ITotalTransformation
40: {
41:         private ADefaultClassDeclIR clazz = null;
42:
43:         private IRInfo info;
44:         private TransAssistantIR transAssistant;
45:         private List<AModuleDeclIR> allModules;
46:
47:         private Logger log = Logger.getLogger(this.getClass().getName());
48:
49:         public ModuleToClassTransformation(IRInfo info,
50:                         TransAssistantIR transAssistant, List<AModuleDeclIR> allModules)
51:         {
52:                 this.info = info;
53:                 this.transAssistant = transAssistant;
54:                 this.allModules = allModules;
55:         }
56:
57:         @Override
58:         public void caseAModuleDeclIR(AModuleDeclIR node) throws AnalysisException
59:         {
60:                 clazz = new ADefaultClassDeclIR();
61:                 clazz.setSourceNode(node.getSourceNode());
62:                 clazz.setAccess(IRConstants.PUBLIC);
63:                 clazz.setName(node.getName());
64:
65:                 // Prevent instantiation of the class
66:                 AMethodDeclIR privConstructor = info.getDeclAssistant().consDefaultContructor(node.getName());
67:                 privConstructor.setAccess(IRConstants.PRIVATE);
68:                 clazz.getMethods().add(privConstructor);
69:
70:                 makeStateAccessExplicit(node);
71:                 handleImports(node.getImport(), clazz);
72:
73:                 // Wrap declarations in a new list to avoid a concurrent modifications
74:                 // exception when moving the module declarations to the class
75:•                for (SDeclIR decl : new LinkedList<>(node.getDecls()))
76:                 {
77:•                        if (decl instanceof AMethodDeclIR)
78:                         {
79:                                 AMethodDeclIR method = (AMethodDeclIR) decl;
80:                                 method.setAccess(IRConstants.PUBLIC);
81:                                 method.setStatic(true);
82:
83:                                 clazz.getMethods().add(method);
84:
85:•                        } else if (decl instanceof AFuncDeclIR)
86:                         {
87:                                 // Functions are static by definition
88:                                 AFuncDeclIR func = (AFuncDeclIR) decl;
89:                                 func.setAccess(IRConstants.PUBLIC);
90:
91:                                 clazz.getFunctions().add(func);
92:
93:•                        } else if (decl instanceof ATypeDeclIR)
94:                         {
95:                                 ATypeDeclIR typeDecl = (ATypeDeclIR) decl;
96:                                 typeDecl.setAccess(IRConstants.PUBLIC);
97:
98:                                 clazz.getTypeDecls().add(typeDecl);
99:
100:•                        } else if (decl instanceof AStateDeclIR)
101:                         {
102:                                 // Handle this as the last thing since it may depend on value definitions
103:                                 continue;
104:•                        } else if (decl instanceof ANamedTraceDeclIR)
105:                         {
106:                                 clazz.getTraces().add((ANamedTraceDeclIR) decl);
107:
108:•                        } else if (decl instanceof AFieldDeclIR)
109:                         {
110:                                 AFieldDeclIR field = (AFieldDeclIR) decl;
111:                                 field.setAccess(IRConstants.PUBLIC);
112:                                 field.setStatic(true);
113:
114:                                 clazz.getFields().add(field);
115:                         } else
116:                         {
117:                                 log.error("Got unexpected declaration: " + decl);
118:                         }
119:                 }
120:
121:                 AStateDeclIR stateDecl = getStateDecl(node);
122:
123:•                if (stateDecl != null)
124:                 {
125:                         ARecordDeclIR record = new ARecordDeclIR();
126:                         record.setSourceNode(stateDecl.getSourceNode());
127:                         record.setName(stateDecl.getName());
128:
129:•                        if (stateDecl.getInvDecl() != null)
130:                         {
131:                                 // The state invariant constrains the type of the state
132:                                 // see https://github.com/overturetool/overture/issues/459
133:                                 record.setInvariant(stateDecl.getInvDecl().clone());
134:•                                if(info.getSettings().addStateInvToModule()) {
135:                                         clazz.getFunctions().add(stateDecl.getInvDecl().clone());
136:                                 }
137:                         }
138:
139:•                        for (AFieldDeclIR field : stateDecl.getFields())
140:                         {
141:                                 record.getFields().add(field.clone());
142:                         }
143:
144:                         ATypeDeclIR typeDecl = new ATypeDeclIR();
145:                         typeDecl.setAccess(IRConstants.PUBLIC);
146:                         typeDecl.setDecl(record);
147:
148:                         clazz.getTypeDecls().add(typeDecl);
149:
150:                         ATypeNameIR typeName = new ATypeNameIR();
151:                         typeName.setName(stateDecl.getName());
152:                         typeName.setDefiningClass(clazz.getName());
153:
154:                         ARecordTypeIR stateType = new ARecordTypeIR();
155:                         stateType.setName(typeName);
156:
157:                         // The state field can't be final since you are allow to assign to it in
158:                         // VDM-SL, e.g. St := mk_St(...)
159:                         clazz.getFields().add(transAssistant.consField(IRConstants.PRIVATE, stateType, stateDecl.getName(), getInitExp(stateDecl)));
160:                 }
161:
162:                 info.removeModule(node.getName());
163:                 info.addClass(clazz);
164:         }
165:
166:         private void handleImports(final AModuleImportsIR moduleImports,
167:                         final ADefaultClassDeclIR clazz) throws AnalysisException
168:         {
169:                 // name = moduleImports.getName();
170:
171:•                if (moduleImports == null)
172:                 {
173:                         return;
174:                 }
175:
176:•                for (AFromModuleImportsIR fromImports : moduleImports.getImports())
177:                 {
178:                         // String fromName = fromImports.getName();
179:
180:•                        for (List<SImportIR> sig : fromImports.getSignatures())
181:                         {
182:•                                for (SImportIR imp : sig)
183:                                 {
184:                                         // TODO Implement the import analysis cases
185:                                         imp.apply(new DepthFirstAnalysisAdaptor()
186:                                         {
187:                                                 @Override
188:                                                 public void caseAAllImportIR(AAllImportIR node)
189:                                                                 throws AnalysisException
190:                                                 {
191:                                                 }
192:
193:                                                 @Override
194:                                                 public void caseATypeImportIR(ATypeImportIR node)
195:                                                                 throws AnalysisException
196:                                                 {
197:                                                 }
198:
199:                                                 @Override
200:                                                 public void caseAFunctionValueImportIR(
201:                                                                 AFunctionValueImportIR node)
202:                                                                 throws AnalysisException
203:                                                 {
204:                                                 }
205:
206:                                                 @Override
207:                                                 public void caseAOperationValueImportIR(
208:                                                                 AOperationValueImportIR node)
209:                                                                 throws AnalysisException
210:                                                 {
211:                                                 }
212:
213:                                                 @Override
214:                                                 public void caseAValueValueImportIR(
215:                                                                 AValueValueImportIR node)
216:                                                                 throws AnalysisException
217:                                                 {
218:                                                         /*
219:                                                          * String renamed = node.getRenamed(); if (renamed != null) { //STypeIR impType =
220:                                                          * node.getImportType(); String from = node.getFromModuleName(); String name =
221:                                                          * node.getName(); AFieldDeclIR impFieldCopy = getValue(name, from).clone();
222:                                                          * impFieldCopy.setAccess(IRConstants.PUBLIC); impFieldCopy.setName(renamed);
223:                                                          * clazz.getFields().add(impFieldCopy);
224:                                                          * //clazz.getFields().add(transAssistant.consConstField(access, type, fromName, initExp));
225:                                                          * }
226:                                                          */
227:                                                 }
228:                                         });
229:                                 }
230:                         }
231:                 }
232:         }
233:
234:         private void makeStateAccessExplicit(final AModuleDeclIR module)
235:                         throws AnalysisException
236:         {
237:                 final AStateDeclIR stateDecl = getStateDecl(module);
238:
239:•                if (stateDecl == null)
240:                 {
241:                         // Nothing to do
242:                         return;
243:                 }
244:
245:                 module.apply(new SlStateAccessTrans(stateDecl, info, transAssistant));
246:         }
247:
248:         public AStateDeclIR getStateDecl(AModuleDeclIR module)
249:         {
250:•                for (SDeclIR decl : module.getDecls())
251:                 {
252:•                        if (decl instanceof AStateDeclIR)
253:                         {
254:                                 return (AStateDeclIR) decl;
255:                         }
256:                 }
257:
258:                 return null;
259:         }
260:
261:         private SExpIR getInitExp(AStateDeclIR stateDecl)
262:         {
263:•                if (stateDecl.getInitExp() instanceof AEqualsBinaryExpIR)
264:                 {
265:                         AEqualsBinaryExpIR eqExp = (AEqualsBinaryExpIR) stateDecl.getInitExp();
266:
267:                         return eqExp.getRight().clone();
268:                 } else
269:                 {
270:                         ANewExpIR defaultRecInit = new ANewExpIR();
271:                         defaultRecInit.setName(transAssistant.getTypeName(stateDecl));
272:                         defaultRecInit.setType(transAssistant.getRecType(stateDecl));
273:
274:•                        for (int i = 0; i < stateDecl.getFields().size(); i++)
275:                         {
276:                                 defaultRecInit.getArgs().add(transAssistant.getInfo().getExpAssistant().consUndefinedExp());
277:                         }
278:
279:                         return defaultRecInit;
280:                 }
281:         }
282:
283:         @SuppressWarnings("unused")
284:         private AFieldDeclIR getValue(String fieldName, String moduleName)
285:         {
286:•                for (AModuleDeclIR module : allModules)
287:                 {
288:•                        if (module.getName().equals(moduleName))
289:                         {
290:•                                for (SDeclIR decl : module.getDecls())
291:                                 {
292:•                                        if (decl instanceof AFieldDeclIR)
293:                                         {
294:                                                 AFieldDeclIR fieldDecl = (AFieldDeclIR) decl;
295:•                                                if (fieldDecl.getName().equals(fieldName))
296:                                                 {
297:                                                         return fieldDecl;
298:                                                 }
299:                                         }
300:                                 }
301:                         }
302:                 }
303:
304:                 log.error("Could not find field " + fieldName + " in module "
305:                                 + moduleName);
306:
307:                 return null;
308:         }
309:
310:         @Override
311:         public PIR getResult()
312:         {
313:                 return clazz;
314:         }
315: }