Package: PossibleTypeFinder

PossibleTypeFinder

nameinstructionbranchcomplexitylinemethod
PossibleTypeFinder(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%
caseABooleanPattern(ABooleanPattern)
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)
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)
M: 1 C: 49
98%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 6
100%
M: 0 C: 1
100%
caseAExpressionPattern(AExpressionPattern)
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)
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%
caseAIgnorePattern(AIgnorePattern)
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)
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseAMapPattern(AMapPattern)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
caseAMapUnionPattern(AMapUnionPattern)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
caseANilPattern(ANilPattern)
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%
caseAObjectPattern(AObjectPattern)
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
caseAQuotePattern(AQuotePattern)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseARealPattern(ARealPattern)
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)
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%
caseASeqPattern(ASeqPattern)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseASetPattern(ASetPattern)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
caseAStringPattern(AStringPattern)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
caseATuplePattern(ATuplePattern)
M: 0 C: 32
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
caseAUnionPattern(AUnionPattern)
M: 1 C: 49
98%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 5
100%
M: 0 C: 1
100%
createNewReturnValue(INode)
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)
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%
defaultPPattern(PPattern)
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
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%
typeOf(long, ILexLocation)
M: 3 C: 14
82%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 4
80%
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 org.overture.ast.analysis.AnalysisException;
25: import org.overture.ast.analysis.AnswerAdaptor;
26: import org.overture.ast.assistant.pattern.PTypeList;
27: import org.overture.ast.factory.AstFactory;
28: import org.overture.ast.intf.lex.ILexLocation;
29: import org.overture.ast.node.INode;
30: import org.overture.ast.patterns.ABooleanPattern;
31: import org.overture.ast.patterns.ACharacterPattern;
32: import org.overture.ast.patterns.AConcatenationPattern;
33: import org.overture.ast.patterns.AExpressionPattern;
34: import org.overture.ast.patterns.AIdentifierPattern;
35: import org.overture.ast.patterns.AIgnorePattern;
36: import org.overture.ast.patterns.AIntegerPattern;
37: import org.overture.ast.patterns.AMapPattern;
38: import org.overture.ast.patterns.AMapUnionPattern;
39: import org.overture.ast.patterns.ANilPattern;
40: import org.overture.ast.patterns.AObjectPattern;
41: import org.overture.ast.patterns.AQuotePattern;
42: import org.overture.ast.patterns.ARealPattern;
43: import org.overture.ast.patterns.ARecordPattern;
44: import org.overture.ast.patterns.ASeqPattern;
45: import org.overture.ast.patterns.ASetPattern;
46: import org.overture.ast.patterns.AStringPattern;
47: import org.overture.ast.patterns.ATuplePattern;
48: import org.overture.ast.patterns.AUnionPattern;
49: import org.overture.ast.patterns.PPattern;
50: import org.overture.ast.types.ASeqSeqType;
51: import org.overture.ast.types.PType;
52: import org.overture.ast.util.PTypeSet;
53: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
54:
55: /**
56: * Used to get if a possible type out of a pattern.
57: *
58: * @author kel
59: */
60:•public class PossibleTypeFinder extends AnswerAdaptor<PType>
61: {
62:         protected ITypeCheckerAssistantFactory af;
63:
64:         public PossibleTypeFinder(ITypeCheckerAssistantFactory af)
65:         {
66:                 this.af = af;
67:         }
68:
69:         @Override
70:         public PType caseABooleanPattern(ABooleanPattern pattern)
71:                         throws AnalysisException
72:         {
73:                 return AstFactory.newABooleanBasicType(pattern.getLocation());
74:         }
75:
76:         @Override
77:         public PType caseACharacterPattern(ACharacterPattern pattern)
78:                         throws AnalysisException
79:         {
80:                 return AstFactory.newACharBasicType(pattern.getLocation());
81:         }
82:
83:         @Override
84:         public PType caseAConcatenationPattern(AConcatenationPattern pattern)
85:                         throws AnalysisException
86:         {
87:                 PTypeSet set = new PTypeSet(af);
88:
89:                 set.add(af.createPPatternAssistant(pattern.getLocation().getModule()).getPossibleType(pattern.getLeft()));
90:                 set.add(af.createPPatternAssistant(pattern.getLocation().getModule()).getPossibleType(pattern.getRight()));
91:
92:                 PType s = set.getType(pattern.getLocation());
93:
94:•                return af.createPTypeAssistant().isUnknown(s) ?
95:                         AstFactory.newASeqSeqType(pattern.getLocation(), AstFactory.newAUnknownType(pattern.getLocation())) : s;
96:         }
97:
98:         @Override
99:         public PType caseAExpressionPattern(AExpressionPattern pattern)
100:                         throws AnalysisException
101:         {
102:                 return AstFactory.newAUnknownType(pattern.getLocation());
103:         }
104:
105:         @Override
106:         public PType caseAIdentifierPattern(AIdentifierPattern pattern)
107:                         throws AnalysisException
108:         {
109:                 return AstFactory.newAUnknownType(pattern.getLocation());
110:         }
111:
112:         @Override
113:         public PType caseAIgnorePattern(AIgnorePattern pattern)
114:                         throws AnalysisException
115:         {
116:                 return AstFactory.newAUnknownType(pattern.getLocation());
117:         }
118:
119:         @Override
120:         public PType caseAIntegerPattern(AIntegerPattern pattern)
121:                         throws AnalysisException
122:         {
123:                 return typeOf(pattern.getValue().getValue(), pattern.getLocation());
124:         }
125:
126:         @Override
127:         public PType caseANilPattern(ANilPattern pattern) throws AnalysisException
128:         {
129:                 return AstFactory.newAOptionalType(pattern.getLocation(), AstFactory.newAUnknownType(pattern.getLocation()));
130:         }
131:
132:         @Override
133:         public PType caseAQuotePattern(AQuotePattern pattern)
134:                         throws AnalysisException
135:         {
136:                 return AstFactory.newAQuoteType(((AQuotePattern) pattern).getValue().clone());
137:         }
138:
139:         @Override
140:         public PType caseARealPattern(ARealPattern pattern)
141:                         throws AnalysisException
142:         {
143:                 return AstFactory.newARealNumericBasicType(pattern.getLocation());
144:         }
145:
146:         @Override
147:         public PType caseARecordPattern(ARecordPattern pattern)
148:                         throws AnalysisException
149:         {
150:                 return pattern.getType();
151:         }
152:
153:         @Override
154:         public PType caseASetPattern(ASetPattern pattern) throws AnalysisException
155:         {
156:                 return AstFactory.newASetSetType(pattern.getLocation(), af.createPPatternListAssistant().getPossibleType(pattern.getPlist(), pattern.getLocation()));
157:         }
158:
159:         @Override
160:         public PType caseASeqPattern(ASeqPattern pattern) throws AnalysisException
161:         {
162:                 return AstFactory.newASeqSeqType(pattern.getLocation(), af.createPPatternListAssistant().getPossibleType(pattern.getPlist(), pattern.getLocation()));
163:         }
164:
165:         @Override
166:         public PType caseAStringPattern(AStringPattern pattern)
167:                         throws AnalysisException
168:         {
169:                 ASeqSeqType t = AstFactory.newASeqSeqType(pattern.getLocation(), AstFactory.newACharBasicType(pattern.getLocation()));
170:                 return t;
171:         }
172:
173:         @Override
174:         public PType caseATuplePattern(ATuplePattern pattern)
175:                         throws AnalysisException
176:         {
177:                 PTypeList list = new PTypeList();
178:
179:•                for (PPattern p : pattern.getPlist())
180:                 {
181:                         list.add(af.createPPatternAssistant(pattern.getLocation().getModule()).getPossibleType(p));
182:                 }
183:
184:                 return list.getType(pattern.getLocation());
185:         }
186:
187:         @Override
188:         public PType caseAUnionPattern(AUnionPattern pattern)
189:                         throws AnalysisException
190:         {
191:                 PTypeSet set = new PTypeSet(af);
192:
193:                 set.add(af.createPPatternAssistant(pattern.getLocation().getModule()).getPossibleType(pattern.getLeft()));
194:                 set.add(af.createPPatternAssistant(pattern.getLocation().getModule()).getPossibleType(pattern.getRight()));
195:
196:                 PType s = set.getType(pattern.getLocation());
197:
198:•                return af.createPTypeAssistant().isUnknown(s) ? AstFactory.newASetSetType(pattern.getLocation(), AstFactory.newAUnknownType(pattern.getLocation()))
199:                                 : s;
200:         }
201:
202:         @Override
203:         public PType caseAMapPattern(AMapPattern pattern) throws AnalysisException
204:         {
205:                 return AstFactory.newAMapMapType(pattern.getLocation(), AstFactory.newAUnknownType(pattern.getLocation()), AstFactory.newAUnknownType(pattern.getLocation()));
206:         }
207:
208:         @Override
209:         public PType caseAMapUnionPattern(AMapUnionPattern pattern) throws AnalysisException
210:         {
211:                 return AstFactory.newAMapMapType(pattern.getLocation(), AstFactory.newAUnknownType(pattern.getLocation()), AstFactory.newAUnknownType(pattern.getLocation()));
212:         }
213:
214:         @Override
215:         public PType caseAObjectPattern(AObjectPattern pattern)
216:                         throws AnalysisException
217:         {
218:                 return pattern.getType();
219:         }
220:
221:         @Override
222:         public PType defaultPPattern(PPattern pattern) throws AnalysisException
223:         {
224:                 return null;
225:         }
226:
227:         @Override
228:         public PType createNewReturnValue(INode node) throws AnalysisException
229:         {
230:•                assert false : "Should not happen";
231:                 return null;
232:         }
233:
234:         @Override
235:         public PType createNewReturnValue(Object node) throws AnalysisException
236:         {
237:•                assert false : "Should not happen";
238:                 return null;
239:         }
240:
241:         private PType typeOf(long value, ILexLocation location)
242:         {
243:•                if (value > 0)
244:                 {
245:                         return AstFactory.newANatOneNumericBasicType(location);
246:•                } else if (value >= 0)
247:                 {
248:                         return AstFactory.newANatNumericBasicType(location);
249:                 } else
250:                 {
251:                         return AstFactory.newAIntNumericBasicType(location);
252:                 }
253:         }
254:
255: }