Package: PDefinitionAssistantTC

PDefinitionAssistantTC

nameinstructionbranchcomplexitylinemethod
PDefinitionAssistantTC(ITypeCheckerAssistantFactory)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
checkDuplicatePatterns(PDefinition, List)
M: 17 C: 59
78%
M: 3 C: 11
79%
M: 3 C: 5
63%
M: 2 C: 10
83%
M: 0 C: 1
100%
deref(PDefinition)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
equals(PDefinition, Object)
M: 3 C: 9
75%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
findName(PDefinition, ILexNameToken, NameScope)
M: 3 C: 12
80%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
findNameBaseCase(PDefinition, ILexNameToken, NameScope)
M: 0 C: 35
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
findType(List, ILexNameToken, String)
M: 0 C: 23
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
findType(PDefinition, ILexNameToken, String)
M: 3 C: 12
80%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
getDefinitions(PDefinition)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
getFreeVariables(PDefinition, FreeVarInfo)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
getSelfDefinition(PDefinition)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
getType(PDefinition)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
getVariableNames(PDefinition)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
hasSupertype(SClassDefinition, PType)
M: 0 C: 39
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 8
100%
M: 0 C: 1
100%
implicitDefinitions(PDefinition, Environment)
M: 1 C: 8
89%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 3
75%
M: 0 C: 1
100%
isCallableOperation(PDefinition)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
isFunction(PDefinition)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
isFunctionOrOperation(PDefinition)
M: 0 C: 16
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
isInstanceVariable(PDefinition)
M: 11 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
isOperation(PDefinition)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
isStatic(PDefinition)
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%
isSubclassResponsibility(PDefinition)
M: 0 C: 52
100%
M: 0 C: 10
100%
M: 0 C: 6
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
isTypeDefinition(PDefinition)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
isUpdatable(PDefinition)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
isUsed(PDefinition)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
isValueDefinition(PDefinition)
M: 3 C: 8
73%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
kind(PDefinition)
M: 3 C: 7
70%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 2 C: 1
33%
M: 0 C: 1
100%
markUsed(PDefinition)
M: 1 C: 7
88%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 3
75%
M: 0 C: 1
100%
typeResolve(PDefinition, IQuestionAnswer, TypeCheckInfo)
M: 1 C: 12
92%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 3
75%
M: 0 C: 1
100%
unusedCheck(PDefinition)
M: 1 C: 7
88%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 1 C: 3
75%
M: 0 C: 1
100%
unusedCheckBaseCase(PDefinition)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * The VDM Type Checker
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.typechecker.assistant.definition;
23:
24: import java.util.HashSet;
25: import java.util.List;
26: import java.util.Set;
27: import java.util.Vector;
28:
29: import org.overture.ast.analysis.AnalysisException;
30: import org.overture.ast.analysis.intf.IQuestionAnswer;
31: import org.overture.ast.assistant.IAstAssistant;
32: import org.overture.ast.assistant.definition.PDefinitionAssistant;
33: import org.overture.ast.definitions.AExplicitFunctionDefinition;
34: import org.overture.ast.definitions.AExplicitOperationDefinition;
35: import org.overture.ast.definitions.AImplicitFunctionDefinition;
36: import org.overture.ast.definitions.AImplicitOperationDefinition;
37: import org.overture.ast.definitions.AInheritedDefinition;
38: import org.overture.ast.definitions.PDefinition;
39: import org.overture.ast.definitions.SClassDefinition;
40: import org.overture.ast.expressions.ASubclassResponsibilityExp;
41: import org.overture.ast.intf.lex.ILexNameToken;
42: import org.overture.ast.lex.LexNameList;
43: import org.overture.ast.lex.LexNameSet;
44: import org.overture.ast.statements.ASubclassResponsibilityStm;
45: import org.overture.ast.typechecker.NameScope;
46: import org.overture.ast.types.AClassType;
47: import org.overture.ast.types.PType;
48: import org.overture.typechecker.Environment;
49: import org.overture.typechecker.TypeCheckInfo;
50: import org.overture.typechecker.TypeChecker;
51: import org.overture.typechecker.TypeCheckerErrors;
52: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
53: import org.overture.typechecker.utilities.DefinitionFinder;
54: import org.overture.typechecker.utilities.DefinitionTypeResolver;
55: import org.overture.typechecker.utilities.FreeVarInfo;
56: import org.overture.typechecker.utilities.NameFinder;
57:
58: //TODO Add assistant Javadoc
59: /**
60: * Top-Level assistant. Will probably remain present for conveniency's sake but the static access will be disallowed.
61: *
62: * @author ldc
63: */
64: public class PDefinitionAssistantTC extends PDefinitionAssistant implements IAstAssistant
65: {
66:         protected ITypeCheckerAssistantFactory af;
67:
68:         public PDefinitionAssistantTC(ITypeCheckerAssistantFactory af)
69:         {
70:                 super(af);
71:                 this.af = af;
72:         }
73:
74:         public boolean equals(PDefinition d, Object other) // Used for sets of definitions.
75:         {
76:                 try
77:                 {
78:                         return d.apply(af.getDefinitionEqualityChecker(), other);
79:                 } catch (AnalysisException e)
80:                 {
81:                         return false;
82:                 }
83:         }
84:
85:         public boolean hasSupertype(SClassDefinition aClassDefDefinition,
86:                         PType other)
87:         {
88:
89:•                if (af.createPTypeAssistant().equals(af.createPDefinitionAssistant().getType(aClassDefDefinition), other))
90:                 {
91:                         return true;
92:                 } else
93:                 {
94:•                        for (PType type : aClassDefDefinition.getSupertypes())
95:                         {
96:                                 AClassType sclass = (AClassType) type;
97:
98:•                                if (af.createPTypeAssistant().hasSupertype(sclass, other))
99:                                 {
100:                                         return true;
101:                                 }
102:                         }
103:                 }
104:                 return false;
105:
106:         }
107:
108:         public boolean isFunctionOrOperation(PDefinition possible)
109:         {
110:•                return af.createPDefinitionAssistant().isFunction(possible)
111:•                                || af.createPDefinitionAssistant().isOperation(possible);
112:         }
113:
114:         public PDefinition findType(List<PDefinition> definitions,
115:                         ILexNameToken name, String fromModule)
116:         {
117:
118:•                for (PDefinition d : definitions)
119:                 {
120:                         PDefinition def = findType(d, name, fromModule);
121:
122:•                        if (def != null)
123:                         {
124:                                 return def;
125:                         }
126:                 }
127:
128:                 return null;
129:
130:         }
131:
132:         public PDefinition findType(PDefinition d, ILexNameToken sought,
133:                         String fromModule)
134:         {
135:                 try
136:                 {
137:                         return d.apply(af.getDefinitionFinder(), new DefinitionFinder.Newquestion(sought, fromModule));
138:                 } catch (AnalysisException e)
139:                 {
140:                         return null;
141:                 }
142:         }
143:
144:         public PDefinition findName(PDefinition d, ILexNameToken sought,
145:                         NameScope scope)
146:         {
147:
148:                 try
149:                 {
150:                         return d.apply(af.getNameFinder(), new NameFinder.Newquestion(sought, scope));
151:                 } catch (AnalysisException e)
152:                 {
153:                         return null;
154:                 }
155:
156:         }
157:
158:         public PDefinition findNameBaseCase(PDefinition d, ILexNameToken sought,
159:                         NameScope scope)
160:         {
161:•                if (af.getLexNameTokenAssistant().isEqual(d.getName(), sought))
162:                 {
163:•                        if (!d.getNameScope().matches(scope))
164:                         {
165:                                 TypeChecker.report(3302, "State variable '"
166:                                                 + sought.getFullName()
167:                                                 + "' cannot be accessed from this context", sought.getLocation());
168:                         }
169:
170:                         markUsed(d);
171:                         return d;
172:                 }
173:
174:                 return null;
175:
176:         }
177:
178:         public void markUsed(PDefinition d)
179:         {
180:                 try
181:                 {
182:                         d.apply(af.getUsedMarker());
183:                 } catch (AnalysisException e)
184:                 {
185:
186:                 }
187:         }
188:
189:         public void unusedCheck(PDefinition d)
190:         {
191:                 try
192:                 {
193:                         d.apply(af.getUnusedChecker());
194:                 } catch (AnalysisException e)
195:                 {
196:
197:                 }
198:         }
199:
200:         public void unusedCheckBaseCase(PDefinition d)
201:         {
202:•                if (!af.createPDefinitionAssistant().isUsed(d))
203:                 {
204:                         TypeCheckerErrors.warning(5000, "Definition '" + d.getName()
205:                                         + "' not used", d.getLocation(), d);
206:                         markUsed(d); // To avoid multiple warnings
207:                 }
208:
209:         }
210:
211:         public List<PDefinition> getDefinitions(PDefinition d)
212:         {
213:                 try
214:                 {
215:                         return d.apply(af.getDefinitionCollector());
216:                 } catch (AnalysisException e)
217:                 {
218:                         return null;
219:                 }
220:
221:         }
222:
223:         public PDefinition getSelfDefinition(PDefinition d)
224:         {
225:                 try
226:                 {
227:                         return d.apply(af.getSelfDefinitionFinder());
228:                 } catch (AnalysisException e)
229:                 {
230:                         return null;
231:                 }
232:
233:         }
234:
235:         public LexNameList getVariableNames(PDefinition d)
236:         {
237:                 try
238:                 {
239:                         return d.apply(af.getVariableNameCollector());
240:                 } catch (AnalysisException e)
241:                 {
242:                         return null;
243:                 }
244:
245:         }
246:
247:         public boolean isStatic(PDefinition fdef)
248:         {
249:                 return af.createPAccessSpecifierAssistant().isStatic(fdef.getAccess());
250:         }
251:
252:         public PDefinition deref(PDefinition d)
253:         {
254:                 try
255:                 {
256:                         return d.apply(af.getDereferer());
257:                 } catch (AnalysisException e)
258:                 {
259:                         return null;
260:                 }
261:
262:         }
263:
264:         public boolean isCallableOperation(PDefinition d)
265:         {
266:                 try
267:                 {
268:                         return d.apply(af.getCallableOperationChecker());
269:                 } catch (AnalysisException e)
270:                 {
271:                         return false;
272:                 }
273:
274:         }
275:
276:         public boolean isUsed(PDefinition d)
277:         {
278:                 try
279:                 {
280:                         return d.apply(af.getUsedChecker());
281:                 } catch (AnalysisException e)
282:                 {
283:                         return false;
284:                 }
285:
286:         }
287:
288:         public void implicitDefinitions(PDefinition d, Environment env)
289:         {
290:                 try
291:                 {
292:                         d.apply(af.getImplicitDefinitionFinder(), env);
293:                 } catch (AnalysisException e)
294:                 {
295:
296:                 }
297:
298:         }
299:
300:         public void typeResolve(PDefinition d,
301:                         IQuestionAnswer<TypeCheckInfo, PType> rootVisitor,
302:                         TypeCheckInfo question) throws AnalysisException
303:         {
304:                 try
305:                 {
306:                         d.apply(af.getDefinitionTypeResolver(), new DefinitionTypeResolver.NewQuestion(rootVisitor, question));
307:                 } catch (AnalysisException e)
308:                 {
309:
310:                 }
311:
312:         }
313:
314:         public PType getType(PDefinition d)
315:         {
316:                 try
317:                 {
318:                         return d.apply(af.getDefinitionTypeFinder());
319:                 } catch (AnalysisException e)
320:                 {
321:                         return null;
322:                 }
323:
324:         }
325:
326:         public boolean isUpdatable(PDefinition d)
327:         {
328:                 try
329:                 {
330:                         return d.apply(af.getUpdatableChecker());
331:                 } catch (AnalysisException e)
332:                 {
333:                         return false;
334:                 }
335:         }
336:
337:         public String kind(PDefinition d)
338:         {
339:                 try
340:                 {
341:                         return d.apply(af.getKindFinder());
342:                 } catch (AnalysisException e)
343:                 {
344:                         return null;
345:                 }
346:
347:         }
348:
349:         public boolean isFunction(PDefinition d)
350:         {
351:                 try
352:                 {
353:                         return d.apply(af.getFunctionChecker());
354:                 } catch (AnalysisException e)
355:                 {
356:                         return false;
357:                 }
358:         }
359:
360:         public boolean isTypeDefinition(PDefinition d)
361:         {
362:                 try
363:                 {
364:                         return d.apply(af.getTypeDefinitionChecker());
365:                 } catch (AnalysisException e)
366:                 {
367:                         return false;
368:                 }
369:         }
370:
371:         public boolean isValueDefinition(PDefinition d)
372:         {
373:                 try
374:                 {
375:                         return d.apply(af.getValueDefinitionChecker());
376:                 } catch (AnalysisException e)
377:                 {
378:                         return false;
379:                 }
380:         }
381:
382:         public boolean isOperation(PDefinition d)
383:         {
384:                 try
385:                 {
386:                         return d.apply(af.getOperationChecker());
387:                 } catch (AnalysisException e)
388:                 {
389:                         return false;
390:                 }
391:         }
392:
393:         /**
394:          * Check a DefinitionList for incompatible duplicate pattern definitions.
395:          *
396:          * @param d
397:          * @param defs
398:          * @return
399:          */
400:         public List<PDefinition> checkDuplicatePatterns(PDefinition d,
401:                         List<PDefinition> defs)
402:         {
403:                 Set<PDefinition> noDuplicates = new HashSet<PDefinition>();
404:
405:•                for (PDefinition d1 : defs)
406:                 {
407:•                        for (PDefinition d2 : defs)
408:                         {
409:•                                if (d1 != d2 && d1.getName() != null && d2.getName() != null
410:•                                                && d1.getName().equals(d2.getName()))
411:                                 {
412:•                                        if (!af.getTypeComparator().compatible(d1.getType(), d2.getType()))
413:                                         {
414:                                                 TypeCheckerErrors.report(3322, "Duplicate patterns bind to different types", d.getLocation(), d);
415:                                                 TypeCheckerErrors.detail2(d1.getName().getName(), d1.getType(), d2.getName().getName(), d2.getType());
416:                                         }
417:                                 }
418:                         }
419:
420:                         noDuplicates.add(d1);
421:                 }
422:
423:                 return new Vector<PDefinition>(noDuplicates);
424:         }
425:
426:         public boolean isSubclassResponsibility(PDefinition d)
427:         {
428:•                while (d instanceof AInheritedDefinition)
429:                 {
430:                         AInheritedDefinition aid = (AInheritedDefinition) d;
431:                         d = aid.getSuperdef();
432:                 }
433:
434:•                if (d instanceof AExplicitOperationDefinition)
435:                 {
436:                         AExplicitOperationDefinition op = (AExplicitOperationDefinition) d;
437:                         return op.getBody() instanceof ASubclassResponsibilityStm;
438:•                } else if (d instanceof AImplicitOperationDefinition)
439:                 {
440:                         AImplicitOperationDefinition op = (AImplicitOperationDefinition) d;
441:                         return op.getBody() instanceof ASubclassResponsibilityStm;
442:•                } else if (d instanceof AExplicitFunctionDefinition)
443:                 {
444:                         AExplicitFunctionDefinition fn = (AExplicitFunctionDefinition) d;
445:                         return fn.getBody() instanceof ASubclassResponsibilityExp;
446:•                } else if (d instanceof AImplicitFunctionDefinition)
447:                 {
448:                         AImplicitFunctionDefinition fn = (AImplicitFunctionDefinition) d;
449:                         return fn.getBody() instanceof ASubclassResponsibilityExp;
450:                 }
451:
452:                 return false;
453:         }
454:
455:         public boolean isInstanceVariable(PDefinition def)
456:         {
457:                 try
458:                 {
459:                         return def.apply(af.getInstanceVariableChecker());
460:                 } catch (AnalysisException e)
461:                 {
462:                         return false;
463:                 }
464:         }
465:
466:         public LexNameSet getFreeVariables(PDefinition def, FreeVarInfo empty)
467:         {
468:                 try
469:                 {
470:                         return def.apply(af.getFreeVariablesChecker(), empty);
471:                 } catch (AnalysisException e)
472:                 {
473:                         return null;
474:                 }
475:         }
476: }