Package: PDefinitionListAssistantTC

PDefinitionListAssistantTC

nameinstructionbranchcomplexitylinemethod
PDefinitionListAssistantTC(ITypeCheckerAssistantFactory)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
findMatches(List, ILexNameToken)
M: 0 C: 34
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
findName(List, ILexNameToken, NameScope)
M: 0 C: 25
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
findStateDefinition(List)
M: 0 C: 19
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
findType(List, ILexNameToken, String)
M: 0 C: 25
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getVariableNames(List)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
hasSubclassResponsibilities(List)
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%
implicitDefinitions(List, Environment)
M: 0 C: 18
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
initializedCheck(AInstanceVariableDefinition)
M: 0 C: 28
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 5
100%
M: 0 C: 1
100%
initializedCheck(LinkedList)
M: 0 C: 21
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
markUsed(List)
M: 17 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
removeAbstracts(List)
M: 0 C: 80
100%
M: 0 C: 14
100%
M: 0 C: 8
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
removeDuplicates(List)
M: 0 C: 76
100%
M: 2 C: 12
86%
M: 2 C: 6
75%
M: 0 C: 19
100%
M: 0 C: 1
100%
setAccessibility(List, AAccessSpecifierAccessSpecifier)
M: 0 C: 16
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setClassDefinition(List, SClassDefinition)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
singleDefinitions(List)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
typeCheck(List, IQuestionAnswer, TypeCheckInfo)
M: 6 C: 26
81%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 1 C: 5
83%
M: 0 C: 1
100%
typeResolve(List, IQuestionAnswer, TypeCheckInfo)
M: 0 C: 35
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
unusedCheck(List)
M: 0 C: 17
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
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.ArrayList;
25: import java.util.HashSet;
26: import java.util.LinkedList;
27: import java.util.List;
28: import java.util.Set;
29: import java.util.Vector;
30:
31: import org.overture.ast.analysis.AnalysisException;
32: import org.overture.ast.analysis.intf.IQuestionAnswer;
33: import org.overture.ast.assistant.IAstAssistant;
34: import org.overture.ast.definitions.AInheritedDefinition;
35: import org.overture.ast.definitions.AInstanceVariableDefinition;
36: import org.overture.ast.definitions.AStateDefinition;
37: import org.overture.ast.definitions.PDefinition;
38: import org.overture.ast.definitions.SClassDefinition;
39: import org.overture.ast.intf.lex.ILexNameToken;
40: import org.overture.ast.lex.LexNameList;
41: import org.overture.ast.typechecker.NameScope;
42: import org.overture.ast.types.AAccessSpecifierAccessSpecifier;
43: import org.overture.ast.types.PType;
44: import org.overture.typechecker.Environment;
45: import org.overture.typechecker.TypeCheckException;
46: import org.overture.typechecker.TypeCheckInfo;
47: import org.overture.typechecker.TypeCheckerErrors;
48: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
49:
50: public class PDefinitionListAssistantTC implements IAstAssistant
51: {
52:         protected ITypeCheckerAssistantFactory af;
53:
54:         public PDefinitionListAssistantTC(ITypeCheckerAssistantFactory af)
55:         {
56:                 this.af = af;
57:         }
58:
59:         public void implicitDefinitions(List<PDefinition> paramDefinitions,
60:                         Environment env)
61:         {
62:•                for (PDefinition d : paramDefinitions)
63:                 {
64:                         af.createPDefinitionAssistant().implicitDefinitions(d, env);
65:                         // System.out.println();
66:                 }
67:
68:         }
69:
70:         public PDefinition findName(List<PDefinition> definitions,
71:                         ILexNameToken name, NameScope scope)
72:         {
73:•                for (PDefinition d : definitions)
74:                 {
75:                         PDefinition def = af.createPDefinitionAssistant().findName(d, name, scope);
76:
77:•                        if (def != null)
78:                         {
79:                                 return def;
80:                         }
81:                 }
82:
83:                 return null;
84:         }
85:
86:         public AStateDefinition findStateDefinition(List<PDefinition> definitions)
87:         {
88:•                for (PDefinition d : definitions)
89:                 {
90:•                        if (d instanceof AStateDefinition)
91:                         {
92:                                 return (AStateDefinition) d;
93:                         }
94:                 }
95:
96:                 return null;
97:         }
98:
99:         public void unusedCheck(List<PDefinition> definitions)
100:         {
101:•                for (PDefinition d : definitions)
102:                 {
103:                         af.createPDefinitionAssistant().unusedCheck(d);
104:                 }
105:
106:         }
107:
108:         public Set<PDefinition> findMatches(List<PDefinition> definitions,
109:                         ILexNameToken name)
110:         {
111:
112:                 Set<PDefinition> set = new HashSet<PDefinition>();
113:
114:•                for (PDefinition d : singleDefinitions(definitions))
115:                 {
116:•                        if (af.createPDefinitionAssistant().isFunctionOrOperation(d)
117:•                                        && d.getName().matches(name))
118:                         {
119:                                 set.add(d);
120:                         }
121:                 }
122:
123:                 return set;
124:         }
125:
126:         public List<PDefinition> singleDefinitions(List<PDefinition> definitions)
127:         {
128:                 List<PDefinition> all = new ArrayList<PDefinition>();
129:
130:•                for (PDefinition d : definitions)
131:                 {
132:                         all.addAll(af.createPDefinitionAssistant().getDefinitions(d));
133:                 }
134:
135:                 return all;
136:         }
137:
138:         public void markUsed(List<PDefinition> definitions)
139:         {
140:•                for (PDefinition d : definitions)
141:                 {
142:                         af.createPDefinitionAssistant().markUsed(d);
143:                 }
144:
145:         }
146:
147:         public void typeCheck(List<PDefinition> defs,
148:                         IQuestionAnswer<TypeCheckInfo, PType> rootVisitor,
149:                         TypeCheckInfo question) throws AnalysisException
150:         {
151:•                for (PDefinition d : defs)
152:                 {
153:•                        if (d.getName() != null && d.getName().getName().equals("RESULT"))
154:                         {
155:                                 TypeCheckerErrors.report(3336, "Illegal use of RESULT reserved identifier", d.getLocation(), d);
156:                         }
157:                         
158:                         d.apply(rootVisitor, question);
159:                 }
160:         }
161:
162:         public LexNameList getVariableNames(List<PDefinition> list)
163:         {
164:
165:                 LexNameList variableNames = new LexNameList();
166:
167:•                for (PDefinition d : list)
168:                 {
169:                         variableNames.addAll(af.createPDefinitionAssistant().getVariableNames(d));
170:                 }
171:
172:                 return variableNames;
173:         }
174:
175:         public void setAccessibility(List<PDefinition> defs,
176:                         AAccessSpecifierAccessSpecifier access)
177:         {
178:•                for (PDefinition d : defs)
179:                 {
180:                         d.setAccess(access.clone());
181:                 }
182:
183:         }
184:
185:         public void typeResolve(List<PDefinition> definitions,
186:                         IQuestionAnswer<TypeCheckInfo, PType> rootVisitor,
187:                         TypeCheckInfo question) throws AnalysisException
188:         {
189:                 TypeCheckException problem = null;
190:                 
191:•                for (PDefinition definition : definitions)
192:                 {
193:                         try
194:                         {
195:                                 af.createPDefinitionAssistant().typeResolve(definition, rootVisitor, question);
196:                         }
197:                         catch (TypeCheckException te)
198:                         {
199:•                                if (problem == null)
200:                                 {
201:                                         problem = te;
202:                                 }
203:                                 else
204:                                 {
205:                                         problem.addExtra(te);
206:                                 }
207:                         }
208:                 }
209:                 
210:•                if (problem != null)
211:                 {
212:                         throw problem;
213:                 }
214:         }
215:
216:         public PDefinition findType(List<PDefinition> actualDefs,
217:                         ILexNameToken name, String fromModule)
218:         {
219:•                for (PDefinition d : actualDefs)
220:                 {
221:                         PDefinition def = af.createPDefinitionAssistant().findType(d, name, fromModule);
222:
223:•                        if (def != null)
224:                         {
225:                                 return def;
226:                         }
227:                 }
228:
229:                 return null;
230:         }
231:
232:         public void initializedCheck(LinkedList<PDefinition> definitions)
233:         {
234:•                for (PDefinition d : definitions)
235:                 {
236:•                        if (d instanceof AInstanceVariableDefinition)
237:                         {
238:                                 AInstanceVariableDefinition ivd = (AInstanceVariableDefinition) d;
239:                                 initializedCheck(ivd);
240:                         }
241:                 }
242:         }
243:
244:         public void setClassDefinition(List<PDefinition> defs,
245:                         SClassDefinition classDefinition)
246:         {
247:                 af.createPDefinitionAssistant().setClassDefinition(defs, classDefinition);
248:
249:         }
250:
251:         public boolean hasSubclassResponsibilities(List<PDefinition> definitions)
252:         {
253:                 PDefinitionAssistantTC assistant = af.createPDefinitionAssistant();
254:
255:•                for (PDefinition d : definitions)
256:                 {
257:•                        if (assistant.isSubclassResponsibility(d))
258:                         {
259:                                 return true;
260:                         }
261:                 }
262:
263:                 return false;
264:         }
265:
266:         public void removeDuplicates(List<PDefinition> definitions)
267:         {
268:                 LinkedList<PDefinition> fixed = new LinkedList<PDefinition>();
269:
270:•                for (PDefinition d : definitions)
271:                 {
272:                         boolean found = false;
273:
274:•                        if (d instanceof AInheritedDefinition)
275:                         {
276:                                 AInheritedDefinition indef = (AInheritedDefinition)d;
277:                                 List<PType> q = indef.getSuperdef().getName().getTypeQualifier();
278:                                 indef.getName().setTypeQualifier(q);
279:                         }
280:
281:•                        for (PDefinition e : fixed)
282:                         {
283:•                                if (e.getName() != null && af.getLexNameTokenAssistant().isEqual(e.getName(), d.getName()))
284:                                 {
285:                                         found = true;
286:                                         break;
287:                                 }
288:                         }
289:
290:•                        if (!found)
291:                         {
292:                                 fixed.add(d);
293:                         }
294:                 }
295:
296:•                if (fixed.size() < definitions.size())
297:                 {
298:                         definitions.clear();
299:                         definitions.addAll(fixed);
300:                 }
301:         }
302:         
303:         
304:         public List<PDefinition> removeAbstracts(List<PDefinition> list)
305:         {
306:                 List<PDefinition> keep = new Vector<PDefinition>();
307:                 PDefinitionAssistantTC assistant = af.createPDefinitionAssistant();
308:                 
309:•                for (PDefinition def: list)
310:                 {
311:•                        if (assistant.isSubclassResponsibility(def))
312:                         {
313:                                 boolean found = false;
314:                                 
315:•                                for (PDefinition def2: list)
316:                                 {
317:•                                        if (def2 instanceof AInheritedDefinition)
318:                                         {
319:                                                 AInheritedDefinition indef2 = (AInheritedDefinition)def2;
320:                                                 List<PType> q = indef2.getSuperdef().getName().getTypeQualifier();
321:                                                 indef2.getName().setTypeQualifier(q);
322:                                         }
323:                                         
324:•                                        if (!assistant.isSubclassResponsibility(def2) &&
325:•                                                af.createPDefinitionAssistant().findName(def, def2.getName(), NameScope.NAMESANDSTATE) != null)
326:                                         {
327:                                                 found = true;
328:                                                 break;
329:                                         }
330:                                 }
331:                                 
332:•                                if (!found)
333:                                 {
334:                                         keep.add(def);
335:                                 }
336:                         }
337:                         else
338:                         {
339:                                 keep.add(def);
340:                         }
341:                 }
342:                 
343:                 return keep;
344:         }
345:         
346:         public void initializedCheck(AInstanceVariableDefinition ivd)
347:         {
348:•                if (!ivd.getInitialized()
349:•                                && !af.createPAccessSpecifierAssistant().isStatic(ivd.getAccess()))
350:                 {
351:                         TypeCheckerErrors.warning(5001, "Instance variable '"
352:                                         + ivd.getName() + "' is not initialized", ivd.getLocation(), ivd);
353:                 }
354:
355:         }
356: }