Package: PatternTypeFinder

PatternTypeFinder

nameinstructionbranchcomplexitylinemethod
PatternTypeFinder(IRInfo)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
caseABoolPatternIR(ABoolPatternIR, STypeIR)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
caseACharPatternIR(ACharPatternIR, STypeIR)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
caseAIdentifierPatternIR(AIdentifierPatternIR, STypeIR)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
caseAIgnorePatternIR(AIgnorePatternIR, STypeIR)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
caseAIntPatternIR(AIntPatternIR, STypeIR)
M: 33 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
caseANullPatternIR(ANullPatternIR, STypeIR)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
caseAQuotePatternIR(AQuotePatternIR, STypeIR)
M: 13 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
caseARealPatternIR(ARealPatternIR, STypeIR)
M: 7 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
caseARecordPatternIR(ARecordPatternIR, STypeIR)
M: 13 C: 58
82%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 12
86%
M: 0 C: 1
100%
caseAStringPatternIR(AStringPatternIR, STypeIR)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
caseATuplePatternIR(ATuplePatternIR, STypeIR)
M: 25 C: 49
66%
M: 5 C: 5
50%
M: 4 C: 2
33%
M: 4 C: 11
73%
M: 0 C: 1
100%
defaultSPatternIR(SPatternIR, STypeIR)
M: 15 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
getPatternType(SPatternIR)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getType(PatternTypeFinder, AIdentifierPatternIR)
M: 15 C: 11
42%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 2 C: 4
67%
M: 0 C: 1
100%
static {...}
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%
storeType(SPatternIR, STypeIR)
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%

Coverage

1: package org.overture.codegen.traces;
2:
3: import java.util.IdentityHashMap;
4:
5: import org.apache.log4j.Logger;
6: import org.overture.codegen.ir.IRInfo;
7: import org.overture.codegen.ir.SPatternIR;
8: import org.overture.codegen.ir.STypeIR;
9: import org.overture.codegen.ir.analysis.AnalysisException;
10: import org.overture.codegen.ir.analysis.QuestionAdaptor;
11: import org.overture.codegen.ir.declarations.ARecordDeclIR;
12: import org.overture.codegen.ir.patterns.ABoolPatternIR;
13: import org.overture.codegen.ir.patterns.ACharPatternIR;
14: import org.overture.codegen.ir.patterns.AIdentifierPatternIR;
15: import org.overture.codegen.ir.patterns.AIgnorePatternIR;
16: import org.overture.codegen.ir.patterns.AIntPatternIR;
17: import org.overture.codegen.ir.patterns.ANullPatternIR;
18: import org.overture.codegen.ir.patterns.AQuotePatternIR;
19: import org.overture.codegen.ir.patterns.ARealPatternIR;
20: import org.overture.codegen.ir.patterns.ARecordPatternIR;
21: import org.overture.codegen.ir.patterns.AStringPatternIR;
22: import org.overture.codegen.ir.patterns.ATuplePatternIR;
23: import org.overture.codegen.ir.types.ABoolBasicTypeIR;
24: import org.overture.codegen.ir.types.ACharBasicTypeIR;
25: import org.overture.codegen.ir.types.AIntNumericBasicTypeIR;
26: import org.overture.codegen.ir.types.ANat1NumericBasicTypeIR;
27: import org.overture.codegen.ir.types.ANatNumericBasicTypeIR;
28: import org.overture.codegen.ir.types.AQuoteTypeIR;
29: import org.overture.codegen.ir.types.ARealNumericBasicTypeIR;
30: import org.overture.codegen.ir.types.ARecordTypeIR;
31: import org.overture.codegen.ir.types.ATupleTypeIR;
32: import org.overture.codegen.ir.types.AUnionTypeIR;
33: import org.overture.codegen.ir.types.AUnknownTypeIR;
34:
35: public class PatternTypeFinder extends QuestionAdaptor<STypeIR>
36: {
37:         private IRInfo info;
38:         private IdentityHashMap<SPatternIR, STypeIR> typeTable;
39:
40:         private static Logger log = Logger.getLogger(PatternTypeFinder.class.getName());
41:
42:         public PatternTypeFinder(IRInfo info)
43:         {
44:                 this.info = info;
45:                 this.typeTable = new IdentityHashMap<>();
46:         }
47:
48:         @Override
49:         public void defaultSPatternIR(SPatternIR node, STypeIR question)
50:                         throws AnalysisException
51:         {
52:                 storeType(node, question);
53:                 log.error("Got unexpected pattern: " + node);
54:         }
55:
56:         @Override
57:         public void caseAIdentifierPatternIR(AIdentifierPatternIR node,
58:                         STypeIR question) throws AnalysisException
59:         {
60:                 storeType(node, question);
61:         }
62:
63:         @Override
64:         public void caseAIgnorePatternIR(AIgnorePatternIR node, STypeIR question)
65:                         throws AnalysisException
66:         {
67:                 storeType(node, question);
68:         }
69:
70:         @Override
71:         public void caseABoolPatternIR(ABoolPatternIR node, STypeIR question)
72:                         throws AnalysisException
73:         {
74:                 storeType(node, new ABoolBasicTypeIR());
75:         }
76:
77:         @Override
78:         public void caseACharPatternIR(ACharPatternIR node, STypeIR question)
79:                         throws AnalysisException
80:         {
81:                 storeType(node, new ACharBasicTypeIR());
82:         }
83:
84:         @Override
85:         public void caseAIntPatternIR(AIntPatternIR node, STypeIR question)
86:                         throws AnalysisException
87:         {
88:                 long value = node.getValue();
89:
90:•                if (value > 0)
91:                 {
92:                         storeType(node, new ANat1NumericBasicTypeIR());
93:•                } else if (value >= 0)
94:                 {
95:                         storeType(node, new ANatNumericBasicTypeIR());
96:                 } else
97:                 {
98:                         storeType(node, new AIntNumericBasicTypeIR());
99:                 }
100:         }
101:
102:         @Override
103:         public void caseANullPatternIR(ANullPatternIR node, STypeIR question)
104:                         throws AnalysisException
105:         {
106:                 storeType(node, new AUnknownTypeIR());
107:         }
108:
109:         @Override
110:         public void caseAQuotePatternIR(AQuotePatternIR node, STypeIR question)
111:                         throws AnalysisException
112:         {
113:                 AQuoteTypeIR quoteTypeCg = new AQuoteTypeIR();
114:                 quoteTypeCg.setValue(node.getValue());
115:
116:                 storeType(node, quoteTypeCg);
117:         }
118:
119:         @Override
120:         public void caseARealPatternIR(ARealPatternIR node, STypeIR question)
121:                         throws AnalysisException
122:         {
123:                 storeType(node, new ARealNumericBasicTypeIR());
124:         }
125:
126:         @Override
127:         public void caseAStringPatternIR(AStringPatternIR node, STypeIR question)
128:                         throws AnalysisException
129:         {
130:                 storeType(node, question);
131:         }
132:
133:         @Override
134:         public void caseATuplePatternIR(ATuplePatternIR node, STypeIR question)
135:                         throws AnalysisException
136:         {
137:                 ATupleTypeIR tupType = null;
138:
139:•                if (question instanceof ATupleTypeIR)
140:                 {
141:                         tupType = (ATupleTypeIR) question;
142:
143:•                } else if (question instanceof AUnionTypeIR)
144:                 {
145:                         tupType = info.getPatternAssistant().getTupleType((AUnionTypeIR) question, node);
146:                 }
147:
148:                 storeType(node, question);
149:
150:•                if (tupType != null)
151:                 {
152:
153:•                        if (tupType.getTypes().size() == node.getPatterns().size())
154:                         {
155:•                                for (int i = 0; i < node.getPatterns().size(); i++)
156:                                 {
157:                                         SPatternIR p = node.getPatterns().get(i);
158:                                         STypeIR t = tupType.getTypes().get(i);
159:
160:                                         p.apply(this, t);
161:                                 }
162:                         } else
163:                         {
164:                                 log.error("Problem encountered when determining the type of a tuple pattern. Patterns and types do not match in terms of size");
165:                         }
166:
167:                 } else
168:                 {
169:                         log.error("Expected tuple type or union type. Got: " + question);
170:                 }
171:         }
172:
173:         @Override
174:         public void caseARecordPatternIR(ARecordPatternIR node, STypeIR question)
175:                         throws AnalysisException
176:         {
177:                 STypeIR type = node.getType();
178:                 storeType(node, type);
179:
180:•                if (type instanceof ARecordTypeIR)
181:                 {
182:                         ARecordTypeIR recType = (ARecordTypeIR) type;
183:
184:                         ARecordDeclIR rec = info.getDeclAssistant().findRecord(info.getClasses(), recType);
185:
186:•                        if (rec.getFields().size() == node.getPatterns().size())
187:                         {
188:•                                for (int i = 0; i < node.getPatterns().size(); i++)
189:                                 {
190:                                         SPatternIR p = node.getPatterns().get(i);
191:                                         STypeIR t = rec.getFields().get(i).getType();
192:
193:                                         p.apply(this, t);
194:                                 }
195:                         } else
196:                         {
197:                                 log.error("Record patterns and record fields do not match in terms of size");
198:                         }
199:
200:                 } else
201:                 {
202:                         log.error("Expected record pattern to have a record type. Got: "
203:                                         + type);
204:                 }
205:         }
206:
207:         private void storeType(SPatternIR pattern, STypeIR type)
208:         {
209:                 this.typeTable.put(pattern, type);
210:         }
211:
212:         public STypeIR getPatternType(SPatternIR pattern)
213:         {
214:                 return this.typeTable.get(pattern);
215:         }
216:
217:         public static STypeIR getType(PatternTypeFinder typeFinder,
218:                         AIdentifierPatternIR occ)
219:         {
220:                 STypeIR occType = typeFinder.getPatternType(occ);
221:
222:•                if (occType == null)
223:                 {
224:                         log.error("Could not find type of identifier pattern " + occ);
225:                         occType = new AUnknownTypeIR();
226:                 } else
227:                 {
228:                         occType = occType.clone();
229:                 }
230:
231:                 return occType;
232:         }
233: }