Package: AllDefinitionLocator

AllDefinitionLocator

nameinstructionbranchcomplexitylinemethod
AllDefinitionLocator(ITypeCheckerAssistantFactory, String)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
caseABooleanPattern(ABooleanPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseACharacterPattern(ACharacterPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseAConcatenationPattern(AConcatenationPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
caseAExpressionPattern(AExpressionPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseAIdentifierPattern(AIdentifierPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 19
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
caseAIgnorePattern(AIgnorePattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseAIntegerPattern(AIntegerPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseAMapPattern(AMapPattern, AllDefinitionLocator.NewQuestion)
M: 11 C: 51
82%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 8
80%
M: 0 C: 1
100%
caseAMapUnionPattern(AMapUnionPattern, AllDefinitionLocator.NewQuestion)
M: 52 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
caseANilPattern(ANilPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseAObjectPattern(AObjectPattern, AllDefinitionLocator.NewQuestion)
M: 120 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%
caseAQuotePattern(AQuotePattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseARealPattern(ARealPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseARecordPattern(ARecordPattern, AllDefinitionLocator.NewQuestion)
M: 25 C: 84
77%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 6 C: 13
68%
M: 0 C: 1
100%
caseASeqPattern(ASeqPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 67
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
caseASetPattern(ASetPattern, AllDefinitionLocator.NewQuestion)
M: 11 C: 56
84%
M: 1 C: 5
83%
M: 1 C: 3
75%
M: 2 C: 8
80%
M: 0 C: 1
100%
caseAStringPattern(AStringPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseATuplePattern(ATuplePattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 82
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 12
100%
M: 0 C: 1
100%
caseAUnionPattern(AUnionPattern, AllDefinitionLocator.NewQuestion)
M: 0 C: 52
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
createNewReturnValue(INode, AllDefinitionLocator.NewQuestion)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createNewReturnValue(Object, AllDefinitionLocator.NewQuestion)
M: 9 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getDefinitions(AMapletPatternMaplet, SMapType, NameScope)
M: 0 C: 34
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
static {...}
M: 2 C: 6
75%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 1
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.utilities.pattern;
23:
24: import java.util.ArrayList;
25: import java.util.Collection;
26: import java.util.Iterator;
27: import java.util.List;
28: import java.util.Vector;
29:
30: import org.overture.ast.analysis.AnalysisException;
31: import org.overture.ast.analysis.QuestionAnswerAdaptor;
32: import org.overture.ast.definitions.AInstanceVariableDefinition;
33: import org.overture.ast.definitions.PDefinition;
34: import org.overture.ast.definitions.SClassDefinition;
35: import org.overture.ast.factory.AstFactory;
36: import org.overture.ast.node.INode;
37: import org.overture.ast.patterns.ABooleanPattern;
38: import org.overture.ast.patterns.ACharacterPattern;
39: import org.overture.ast.patterns.AConcatenationPattern;
40: import org.overture.ast.patterns.AExpressionPattern;
41: import org.overture.ast.patterns.AIdentifierPattern;
42: import org.overture.ast.patterns.AIgnorePattern;
43: import org.overture.ast.patterns.AIntegerPattern;
44: import org.overture.ast.patterns.AMapPattern;
45: import org.overture.ast.patterns.AMapUnionPattern;
46: import org.overture.ast.patterns.AMapletPatternMaplet;
47: import org.overture.ast.patterns.ANamePatternPair;
48: import org.overture.ast.patterns.ANilPattern;
49: import org.overture.ast.patterns.AObjectPattern;
50: import org.overture.ast.patterns.AQuotePattern;
51: import org.overture.ast.patterns.ARealPattern;
52: import org.overture.ast.patterns.ARecordPattern;
53: import org.overture.ast.patterns.ASeqPattern;
54: import org.overture.ast.patterns.ASetPattern;
55: import org.overture.ast.patterns.AStringPattern;
56: import org.overture.ast.patterns.ATuplePattern;
57: import org.overture.ast.patterns.AUnionPattern;
58: import org.overture.ast.patterns.PPattern;
59: import org.overture.ast.typechecker.NameScope;
60: import org.overture.ast.types.AClassType;
61: import org.overture.ast.types.AFieldField;
62: import org.overture.ast.types.AProductType;
63: import org.overture.ast.types.ARecordInvariantType;
64: import org.overture.ast.types.SSetType;
65: import org.overture.ast.types.PType;
66: import org.overture.ast.types.SMapType;
67: import org.overture.typechecker.TypeCheckerErrors;
68: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
69: import org.overture.typechecker.assistant.definition.PDefinitionAssistantTC;
70: import org.overture.typechecker.assistant.type.PTypeAssistantTC;
71:
72: /**
73: * Get a complete list of all definitions, including duplicates. This method should only be used only by PP
74: *
75: * @author gkanos
76: */
77:
78:•public class AllDefinitionLocator
79:                 extends
80:                 QuestionAnswerAdaptor<AllDefinitionLocator.NewQuestion, List<PDefinition>>
81: {
82:
83:         public static class NewQuestion
84:         {
85:                 PType ptype;
86:                 NameScope scope;
87:
88:                 public NewQuestion(PType ptype, NameScope scope)
89:                 {
90:                         this.ptype = ptype;
91:                         this.scope = scope;
92:                 }
93:         }
94:
95:         protected final ITypeCheckerAssistantFactory af;
96:         protected final String fromModule;
97:
98:         public AllDefinitionLocator(ITypeCheckerAssistantFactory af, String fromModule)
99:         {
100:                 this.af = af;
101:                 this.fromModule = fromModule;
102:         }
103:
104:         @Override
105:         public List<PDefinition> caseAIdentifierPattern(AIdentifierPattern pattern,
106:                         NewQuestion question) throws AnalysisException
107:         {
108:                 List<PDefinition> defs = new ArrayList<PDefinition>();
109:                 defs.add(AstFactory.newALocalDefinition(pattern.getLocation(), pattern.getName().clone(), question.scope, question.ptype));
110:                 return defs;
111:         }
112:
113:         @Override
114:         public List<PDefinition> caseABooleanPattern(ABooleanPattern pattern,
115:                         NewQuestion question) throws AnalysisException
116:         {
117:                 return new Vector<PDefinition>();
118:         }
119:
120:         @Override
121:         public List<PDefinition> caseACharacterPattern(ACharacterPattern pattern,
122:                         NewQuestion question) throws AnalysisException
123:         {
124:                 return new Vector<PDefinition>();
125:         }
126:
127:         @Override
128:         public List<PDefinition> caseAExpressionPattern(AExpressionPattern pattern,
129:                         NewQuestion question) throws AnalysisException
130:         {
131:                 return new Vector<PDefinition>();
132:         }
133:
134:         @Override
135:         public List<PDefinition> caseAIgnorePattern(AIgnorePattern pattern,
136:                         NewQuestion question) throws AnalysisException
137:         {
138:                 return new Vector<PDefinition>();
139:         }
140:
141:         @Override
142:         public List<PDefinition> caseAIntegerPattern(AIntegerPattern pattern,
143:                         NewQuestion question) throws AnalysisException
144:         {
145:                 return new Vector<PDefinition>();
146:         }
147:
148:         @Override
149:         public List<PDefinition> caseANilPattern(ANilPattern pattern,
150:                         NewQuestion question) throws AnalysisException
151:         {
152:                 return new Vector<PDefinition>();
153:         }
154:
155:         @Override
156:         public List<PDefinition> caseAQuotePattern(AQuotePattern pattern,
157:                         NewQuestion question) throws AnalysisException
158:         {
159:                 return new Vector<PDefinition>();
160:         }
161:
162:         @Override
163:         public List<PDefinition> caseARealPattern(ARealPattern node,
164:                         NewQuestion question) throws AnalysisException
165:         {
166:                 return new Vector<PDefinition>();
167:         }
168:
169:         @Override
170:         public List<PDefinition> caseAStringPattern(AStringPattern pattern,
171:                         NewQuestion question) throws AnalysisException
172:         {
173:                 return new Vector<PDefinition>();
174:         }
175:
176:         @Override
177:         public List<PDefinition> caseAConcatenationPattern(
178:                         AConcatenationPattern pattern, NewQuestion question)
179:                         throws AnalysisException
180:         {
181:                 List<PDefinition> list = af.createPPatternAssistant(fromModule).getDefinitions(pattern.getLeft(), question.ptype, question.scope);
182:                 list.addAll(af.createPPatternAssistant(fromModule).getDefinitions(pattern.getRight(), question.ptype, question.scope));
183:                 return list;
184:         }
185:
186:         @Override
187:         public List<PDefinition> caseARecordPattern(ARecordPattern pattern,
188:                         NewQuestion question) throws AnalysisException
189:         {
190:                 List<PDefinition> defs = new Vector<PDefinition>();
191:
192:                 PType type = pattern.getType();
193:
194:•                if (!af.createPTypeAssistant().isTag(type, pattern.getLocation().getModule()))
195:                 {
196:                         TypeCheckerErrors.report(3200, "Mk_ expression is not a record type", pattern.getLocation(), pattern);
197:                         TypeCheckerErrors.detail("Type", type);
198:                         return defs;
199:                 }
200:
201:                 ARecordInvariantType pattype = af.createPTypeAssistant().getRecord(type, pattern.getLocation().getModule());
202:
203:•                if (!af.createPTypeAssistant().isType(question.ptype, pattype.getClass()))
204:                 {
205:                         TypeCheckerErrors.report(3201, "Matching expression is not a compatible record type", pattern.getLocation(), pattern);
206:                         TypeCheckerErrors.detail2("Pattern type", type, "Expression type", question.ptype);
207:                         return defs;
208:                 }
209:
210:                 // RecordType usingrec = (RecordType)using;
211:
212:•                if (pattype.getFields().size() != pattern.getPlist().size())
213:                 {
214:                         TypeCheckerErrors.report(3202, "Record pattern argument/field count mismatch", pattern.getLocation(), pattern);
215:                 } else
216:                 {
217:                         Iterator<AFieldField> patfi = pattype.getFields().iterator();
218:
219:•                        for (PPattern p : pattern.getPlist())
220:                         {
221:                                 AFieldField pf = patfi.next();
222:                                 // defs.addAll(p.getDefinitions(usingrec.findField(pf.tag).type, scope));
223:                                 defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(p, pf.getType(), question.scope));
224:                         }
225:                 }
226:
227:                 return defs;
228:         }
229:
230:         @Override
231:         public List<PDefinition> caseASeqPattern(ASeqPattern pattern,
232:                         NewQuestion question) throws AnalysisException
233:         {
234:                 List<PDefinition> defs = new Vector<PDefinition>();
235:
236:•                if (!af.createPTypeAssistant().isSeq(question.ptype, pattern.getLocation().getModule()))
237:                 {
238:                         TypeCheckerErrors.report(3203, "Sequence pattern is matched against "
239:                                         + question.ptype, pattern.getLocation(), pattern);
240:                 } else
241:                 {
242:                         PType elem = af.createPTypeAssistant().getSeq(question.ptype, pattern.getLocation().getModule()).getSeqof();
243:
244:•                        for (PPattern p : pattern.getPlist())
245:                         {
246:                                 defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(p, elem, question.scope));
247:                         }
248:                 }
249:
250:                 return defs;
251:         }
252:
253:         @Override
254:         public List<PDefinition> caseASetPattern(ASetPattern pattern,
255:                         NewQuestion question) throws AnalysisException
256:         {
257:                 // return ASetPatternAssistantTC.getAllDefinitions(pattern, question.ptype, question.scope);
258:                 List<PDefinition> defs = new Vector<PDefinition>();
259:
260:•                if (!af.createPTypeAssistant().isSet(question.ptype, pattern.getLocation().getModule()))
261:                 {
262:                         TypeCheckerErrors.report(3204, "Set pattern is not matched against set type", pattern.getLocation(), pattern);
263:                         TypeCheckerErrors.detail("Actual type", question.ptype);
264:                 } else
265:                 {
266:                         SSetType set = af.createPTypeAssistant().getSet(question.ptype, pattern.getLocation().getModule());
267:
268:•                        if (!set.getEmpty())
269:                         {
270:•                                for (PPattern p : pattern.getPlist())
271:                                 {
272:                                         defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(p, set.getSetof(), question.scope));
273:                                 }
274:                         }
275:                 }
276:
277:                 return defs;
278:         }
279:
280:         @Override
281:         public List<PDefinition> caseATuplePattern(ATuplePattern pattern,
282:                         NewQuestion question) throws AnalysisException
283:         {
284:                 List<PDefinition> defs = new Vector<PDefinition>();
285:
286:•                if (!af.createPTypeAssistant().isProduct(question.ptype, pattern.getPlist().size(), fromModule))
287:                 {
288:                         TypeCheckerErrors.report(3205, "Matching expression is not a product of cardinality "
289:                                         + pattern.getPlist().size(), pattern.getLocation(), pattern);
290:                         TypeCheckerErrors.detail("Actual", question.ptype);
291:                         return defs;
292:                 }
293:
294:                 AProductType product = af.createPTypeAssistant().getProduct(question.ptype, pattern.getPlist().size(), fromModule);
295:                 Iterator<PType> ti = product.getTypes().iterator();
296:
297:•                for (PPattern p : pattern.getPlist())
298:                 {
299:                         defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(p, ti.next(), question.scope));
300:                 }
301:
302:                 return defs;
303:         }
304:
305:         @Override
306:         public List<PDefinition> caseAUnionPattern(AUnionPattern pattern,
307:                         NewQuestion question) throws AnalysisException
308:         {
309:                 List<PDefinition> defs = new Vector<PDefinition>();
310:
311:•                if (!af.createPTypeAssistant().isSet(question.ptype, pattern.getLocation().getModule()))
312:                 {
313:                         TypeCheckerErrors.report(3206, "Matching expression is not a set type", pattern.getLocation(), pattern);
314:                 }
315:
316:                 defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(pattern.getLeft(), question.ptype, question.scope));
317:                 defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(pattern.getRight(), question.ptype, question.scope));
318:
319:                 return defs;
320:         }
321:
322:         @Override
323:         public List<PDefinition> caseAMapUnionPattern(AMapUnionPattern pattern,
324:                         NewQuestion question) throws AnalysisException
325:         {
326:                 List<PDefinition> defs = new Vector<PDefinition>();
327:
328:•                if (!af.createPTypeAssistant().isMap(question.ptype, pattern.getLocation().getModule()))
329:                 {
330:                         TypeCheckerErrors.report(3315, "Matching expression is not a map type", pattern.getLocation(), pattern);
331:                 }
332:
333:                 defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(pattern.getLeft(), question.ptype, question.scope));
334:                 defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(pattern.getRight(), question.ptype, question.scope));
335:
336:                 return defs;
337:         }
338:
339:         @Override
340:         public List<PDefinition> caseAMapPattern(AMapPattern pattern,
341:                         NewQuestion question) throws AnalysisException
342:         {
343:                 List<PDefinition> defs = new Vector<PDefinition>();
344:
345:•                if (!af.createPTypeAssistant().isMap(question.ptype, pattern.getLocation().getModule()))
346:                 {
347:                         TypeCheckerErrors.report(3314, "Map pattern is not matched against map type", pattern.getLocation(), pattern);
348:                         TypeCheckerErrors.detail("Actual type", question.ptype);
349:                 } else
350:                 {
351:                         SMapType map = af.createPTypeAssistant().getMap(question.ptype, pattern.getLocation().getModule());
352:
353:•                        if (!map.getEmpty())
354:                         {
355:•                                for (AMapletPatternMaplet p : pattern.getMaplets())
356:                                 {
357:                                         defs.addAll(getDefinitions(p, map, question.scope));
358:                                         // defs.addAll(p.apply(THIS, question));
359:                                 }
360:                         }
361:                 }
362:
363:                 return defs;
364:         }
365:
366:         @Override
367:         public List<PDefinition> caseAObjectPattern(AObjectPattern pattern,
368:                         NewQuestion question) throws AnalysisException
369:         {
370:                 List<PDefinition> defs = new Vector<PDefinition>();
371:                 PTypeAssistantTC typeAssistant = af.createPTypeAssistant();
372:                 AClassType pattype = typeAssistant.getClassType(pattern.getType(), null, pattern.getLocation().getModule());
373:                 AClassType exptype = typeAssistant.getClassType(question.ptype, null, pattern.getLocation().getModule());
374:
375:•                if (exptype == null || !af.getTypeComparator().isSubType(pattype, exptype))
376:                 {
377:                         TypeCheckerErrors.report(3333, "Matching expression is not a compatible object type", pattern.getLocation(), question.ptype);
378:                         TypeCheckerErrors.detail2("Pattern type", pattype, "Expression type", exptype);
379:                         return defs;
380:                 }
381:
382:                 SClassDefinition classdef = pattype.getClassdef();
383:                 PDefinitionAssistantTC definitionAssistant = af.createPDefinitionAssistant();
384:
385:•                for (ANamePatternPair npp: pattern.getFields())
386:                 {
387:                         PDefinition d = definitionAssistant.findName(classdef, npp.getName(), NameScope.STATE);        // NB. state lookup
388:                         
389:•                        if (d != null)
390:                         {
391:                                 d = definitionAssistant.deref(d);
392:                         }
393:                         
394:•                        if (d instanceof AInstanceVariableDefinition)
395:                         {
396:                                 defs.addAll(af.createPPatternAssistant(fromModule).getDefinitions(npp.getPattern(), d.getType(), NameScope.LOCAL));
397:                         }
398:                         else
399:                         {
400:                                 TypeCheckerErrors.report(3334, npp.getName().getName() + " is not a matchable field of class " + pattype, npp.getName().getLocation(), npp.getName());
401:                         }
402:                 }
403:
404:                 return defs;
405:         }
406:
407:         @Override
408:         public List<PDefinition> createNewReturnValue(INode node,
409:                         NewQuestion question) throws AnalysisException
410:         {
411:•                assert false : "PPatternAssistant.getDefinitions - should not hit this case";
412:                 return null;
413:         }
414:
415:         @Override
416:         public List<PDefinition> createNewReturnValue(Object node,
417:                         NewQuestion question) throws AnalysisException
418:         {
419:•                assert false : "PPatternAssistant.getDefinitions - should not hit this case";
420:                 return null;
421:         }
422:         
423:         public Collection<? extends PDefinition> getDefinitions(
424:                         AMapletPatternMaplet p, SMapType map, NameScope scope) {
425:
426:                 List<PDefinition> list = new Vector<PDefinition>();
427:                 list.addAll(af.createPPatternAssistant(fromModule).getDefinitions(p.getFrom(),
428:                                 map.getFrom(), scope));
429:                 list.addAll(af.createPPatternAssistant(fromModule).getDefinitions(p.getTo(),
430:                                 map.getTo(), scope));
431:                 return list;
432:         }
433: }