Package: PolyFuncTrans

PolyFuncTrans

nameinstructionbranchcomplexitylinemethod
PolyFuncTrans(TransAssistantIR)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
caseAApplyExpIR(AApplyExpIR)
M: 14 C: 86
86%
M: 7 C: 15
68%
M: 6 C: 6
50%
M: 3 C: 25
89%
M: 0 C: 1
100%
caseAFuncDeclIR(AFuncDeclIR)
M: 0 C: 43
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
consTypeArg(AMethodInstantiationExpIR, STypeIR)
M: 16 C: 125
89%
M: 1 C: 11
92%
M: 1 C: 6
86%
M: 5 C: 36
88%
M: 0 C: 1
100%
consTypeArg(String)
M: 0 C: 35
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
consTypeArgFlag(AMethodInstantiationExpIR, STypeIR)
M: 21 C: 131
86%
M: 6 C: 30
83%
M: 6 C: 13
68%
M: 7 C: 41
85%
M: 0 C: 1
100%
getSetMethodName()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getSetUtil()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getTypeArgumentFieldName()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getUnsupportedTypeFieldName()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getUtilClass()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getVdmSet()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
isSeqOfChar2Val(AExplicitVarExpIR, AClassTypeIR)
M: 0 C: 14
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 2
100%
M: 0 C: 1
100%
issueUnsupportedWarning(AMethodInstantiationExpIR)
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%
toTypeArgName(ATemplateTypeIR)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: package org.overture.codegen.trans;
2:
3: import org.apache.log4j.Logger;
4: import org.overture.ast.definitions.ARenamedDefinition;
5: import org.overture.ast.definitions.PDefinition;
6: import org.overture.ast.expressions.AVariableExp;
7: import org.overture.ast.node.INode;
8: import org.overture.codegen.assistant.AssistantBase;
9: import org.overture.codegen.ir.IRConstants;
10: import org.overture.codegen.ir.SExpIR;
11: import org.overture.codegen.ir.STypeIR;
12: import org.overture.codegen.ir.analysis.AnalysisException;
13: import org.overture.codegen.ir.analysis.DepthFirstAnalysisAdaptor;
14: import org.overture.codegen.ir.declarations.AFormalParamLocalParamIR;
15: import org.overture.codegen.ir.declarations.AFuncDeclIR;
16: import org.overture.codegen.ir.expressions.AApplyExpIR;
17: import org.overture.codegen.ir.expressions.AExplicitVarExpIR;
18: import org.overture.codegen.ir.expressions.AIdentifierVarExpIR;
19: import org.overture.codegen.ir.expressions.AMethodInstantiationExpIR;
20: import org.overture.codegen.ir.expressions.AQuoteLiteralExpIR;
21: import org.overture.codegen.ir.expressions.ATypeArgExpIR;
22: import org.overture.codegen.ir.types.ABoolBasicTypeIR;
23: import org.overture.codegen.ir.types.ACharBasicTypeIR;
24: import org.overture.codegen.ir.types.AClassTypeIR;
25: import org.overture.codegen.ir.types.AExternalTypeIR;
26: import org.overture.codegen.ir.types.AIntNumericBasicTypeIR;
27: import org.overture.codegen.ir.types.AMapMapTypeIR;
28: import org.overture.codegen.ir.types.ANat1NumericBasicTypeIR;
29: import org.overture.codegen.ir.types.ANatNumericBasicTypeIR;
30: import org.overture.codegen.ir.types.AQuoteTypeIR;
31: import org.overture.codegen.ir.types.ARatNumericBasicTypeIR;
32: import org.overture.codegen.ir.types.ARealNumericBasicTypeIR;
33: import org.overture.codegen.ir.types.ARecordTypeIR;
34: import org.overture.codegen.ir.types.ASeqSeqTypeIR;
35: import org.overture.codegen.ir.types.ASetSetTypeIR;
36: import org.overture.codegen.ir.types.AStringTypeIR;
37: import org.overture.codegen.ir.types.ATemplateTypeIR;
38: import org.overture.codegen.ir.types.ATokenBasicTypeIR;
39: import org.overture.codegen.ir.types.AUnionTypeIR;
40: import org.overture.codegen.ir.types.AUnknownTypeIR;
41: import org.overture.codegen.trans.assistants.TransAssistantIR;
42:
43: public class PolyFuncTrans extends DepthFirstAnalysisAdaptor {
44:
45: protected Logger log = Logger.getLogger(this.getClass().getName());
46:
47: private TransAssistantIR assist;
48:
49: private static final String TYPE_ARG_PREFIX = "_type_";
50:
51: private static final String UTIL_CLASS = "Utils";
52: private static final String SET_UTIL = "SetUtil";
53: private static final String VDM_SET = "VDMSet";
54: private static final String SET_METHOD_NAME = "set";
55:
56:
57: public static final String OBJECT = "Object";
58:
59: public static final String NAT = "NAT";
60: public static final String NAT1 = "NAT1";
61: public static final String INT = "INT";
62: public static final String REAL = "REAL";
63: public static final String RAT = "RAT";
64: public static final String BOOL = "BOOL";
65: public static final String CHAR = "CHAR";
66: public static final String TOKEN = "TOKEN";
67: public static final String STRING = "STRING";
68: public static final String SEQ_OF_ANYTHING = "SEQ_OF_ANYTHING";
69: public static final String SET_OF_ANYTHING = "SET_OF_ANYTHING";
70: public static final String MAP_ANYTHING_TO_ANYTHING = "MAP_ANYTHING_TO_ANYTHING";
71: public static final String UNKNOWN = "UNKNOWN";
72:
73: public static final String TYPE_NOT_SUPPORTED = "TYPE_NOT_SUPPORTED";
74:
75: public PolyFuncTrans(TransAssistantIR assist)
76: {
77: this.assist = assist;
78: }
79:
80: @Override
81: public void caseAFuncDeclIR(AFuncDeclIR node) throws AnalysisException {
82:
83: super.caseAFuncDeclIR(node);
84:
85:• if (!node.getTemplateTypes().isEmpty()) {
86:
87:• for(ATemplateTypeIR t : node.getTemplateTypes())
88: {
89: AFormalParamLocalParamIR param = new AFormalParamLocalParamIR();
90: param.setType(new AUnknownTypeIR());
91: param.setPattern(assist.getInfo().getPatternAssistant().consIdPattern(toTypeArgName(t)));
92:
93: node.getFormalParams().add(param);
94: }
95: }
96: }
97:
98: public static String toTypeArgName(ATemplateTypeIR t) {
99:
100: return TYPE_ARG_PREFIX + t;
101: }
102:
103: @Override
104: public void caseAApplyExpIR(AApplyExpIR node) throws AnalysisException {
105:
106: super.caseAApplyExpIR(node);
107:
108: SExpIR root = node.getRoot();
109:
110:• if(root instanceof AMethodInstantiationExpIR)
111: {
112: AMethodInstantiationExpIR methodInst = (AMethodInstantiationExpIR) root;
113:
114: SExpIR func = methodInst.getFunc();
115:
116:• if(func instanceof AExplicitVarExpIR)
117: {
118: AExplicitVarExpIR ev = (AExplicitVarExpIR) func;
119:
120: STypeIR type = ev.getClassType();
121:
122:• if(type instanceof AClassTypeIR)
123: {
124: AClassTypeIR classType = (AClassTypeIR) type;
125:•                                        if(assist.getInfo().getDeclAssistant().isLibraryName(classType.getName())
126:•                                                        && !isSeqOfChar2Val(ev, classType))
127: {
128: // Most libraries don't expect type arguments
129: return;
130: }
131: }
132: }
133:• else if(func instanceof AIdentifierVarExpIR)
134: {
135: INode var = AssistantBase.getVdmNode(func);
136:
137:• if(var instanceof AVariableExp) {
138: PDefinition def = ((AVariableExp) var).getVardef();
139:
140:• if (def instanceof ARenamedDefinition) {
141: ARenamedDefinition renamedDef = (ARenamedDefinition) def;
142:
143:• if (assist.getInfo().getDeclAssistant().isLibraryName(renamedDef.getDef().getName().getModule())) {
144:
145: // Libraries don't expect type arguments
146: return;
147: }
148: }
149:
150: }
151: }
152:
153:• for(STypeIR type : methodInst.getActualTypes())
154: {
155:         SExpIR expToAdd = consTypeArg(methodInst, type);
156:
157:• if(expToAdd != null)
158: {
159:         node.getArgs().add(expToAdd);
160: }
161: }
162: }
163: }
164:
165:         private boolean isSeqOfChar2Val(AExplicitVarExpIR ev, AClassTypeIR classType) {
166:•                return classType.getName().equals(IRConstants.VDMUTIL_LIB) &&
167:•                                ev.getName().equals(IRConstants.SEQ_OF_CHAR2VAL);
168:         }
169:
170:         private SExpIR consTypeArg(AMethodInstantiationExpIR methodInst, STypeIR type) {
171:                 SExpIR expToAdd = null;
172:•                if(type instanceof AQuoteTypeIR)
173:                 {
174:                  AQuoteLiteralExpIR qt = new AQuoteLiteralExpIR();
175:                  qt.setValue(((AQuoteTypeIR) type).getValue());
176:                  expToAdd = qt;
177:                 }
178:•                else if(type instanceof ARecordTypeIR)
179:                 {
180:                  ATypeArgExpIR typeArg = new ATypeArgExpIR();
181:                  typeArg.setType(type.clone());
182:                  expToAdd = typeArg;
183:                 }
184:•                else if(type instanceof ATemplateTypeIR)
185:                 {
186:                  ATemplateTypeIR templateType = (ATemplateTypeIR) type;
187:                  String paramName = toTypeArgName(templateType);
188:                  AIdentifierVarExpIR templateTypeArg = assist.getInfo().getExpAssistant().consIdVar(paramName, templateType.clone());
189:                  expToAdd = templateTypeArg;
190:                 }
191:•                else if(type instanceof AUnionTypeIR)
192:                 {
193:                  AUnionTypeIR unionType = (AUnionTypeIR) type;
194:
195:•                 if(assist.getInfo().getTypeAssistant().isUnionOfNonCollectionTypes(unionType))
196:                  {
197:                  AExplicitVarExpIR setConstructorMember = consTypeArg(getSetUtil());
198:                  setConstructorMember.setName(getSetMethodName());
199:
200:                  AExternalTypeIR setType = new AExternalTypeIR();
201:                  setType.setName(getVdmSet());
202:
203:                  AApplyExpIR setConstructor = new AApplyExpIR();
204:                  setConstructor.setType(setType);
205:                  setConstructor.setRoot(setConstructorMember);
206:
207:•                 for(STypeIR t : unionType.getTypes())
208:                  {
209:                  setConstructor.getArgs().add(consTypeArg(methodInst, t));
210:                  }
211:
212:                  expToAdd = setConstructor;
213:                  }
214:                  else {
215:                  issueUnsupportedWarning(methodInst);
216:                  AExplicitVarExpIR typeArg = consTypeArg(getUtilClass());
217:                  String name = getUnsupportedTypeFieldName();
218:                  typeArg.setName(name);
219:                  expToAdd = typeArg;
220:                  }
221:                 }
222:                 else
223:                 {
224:                  AExplicitVarExpIR typeArg = consTypeArgFlag(methodInst, type);
225:                  expToAdd = typeArg;
226:                 }
227:                 return expToAdd;
228:         }
229:
230:         private AExplicitVarExpIR consTypeArgFlag(AMethodInstantiationExpIR methodInst, STypeIR type) {
231:                 AExplicitVarExpIR typeArg = consTypeArg(getUtilClass());
232:
233:                 String name;
234:•                if(type instanceof ANatNumericBasicTypeIR)
235:                 {
236:                  name = NAT;
237:                 }
238:•                else if(type instanceof ANat1NumericBasicTypeIR)
239:                 {
240:                  name = NAT1;
241:                 }
242:•                else if(type instanceof AIntNumericBasicTypeIR)
243:                 {
244:                  name = INT;
245:                 }
246:•                else if(type instanceof ARealNumericBasicTypeIR)
247:                 {
248:                  name = REAL;
249:                 }
250:•                else if(type instanceof ARatNumericBasicTypeIR)
251:                 {
252:                  name = RAT;
253:                 }
254:•                else if(type instanceof ABoolBasicTypeIR)
255:                 {
256:                  name = BOOL;
257:                 }
258:•                else if(type instanceof ACharBasicTypeIR)
259:                 {
260:                  name = CHAR;
261:                 }
262:•                else if(type instanceof ATokenBasicTypeIR)
263:                 {
264:                  name = TOKEN;
265:                 }
266:•                else if(type instanceof AStringTypeIR)
267:                 {
268:                  name = STRING;
269:                 }
270:•                else if (type instanceof ASeqSeqTypeIR) {
271:                         ASeqSeqTypeIR seqType = ((ASeqSeqTypeIR) type);
272:                         STypeIR seqOf = seqType.getSeqOf();
273:
274:•                        if (seqOf instanceof AUnknownTypeIR && !seqType.getSeq1()) {
275:                                 name = SEQ_OF_ANYTHING;
276:                         } else {
277:                                 issueUnsupportedWarning(methodInst);
278:                                 name = getUnsupportedTypeFieldName();
279:                         }
280:•                } else if (type instanceof ASetSetTypeIR) {
281:                         ASetSetTypeIR setType = ((ASetSetTypeIR) type);
282:                         STypeIR setOf = setType.getSetOf();
283:
284:                         // set1 is not accounted for (it's not supported by the IR)
285:•                        if (setOf instanceof AUnknownTypeIR) {
286:                                 name = SET_OF_ANYTHING;
287:                         } else {
288:                                 issueUnsupportedWarning(methodInst);
289:                                 name = getUnsupportedTypeFieldName();
290:                         }
291:•                } else if (type instanceof AMapMapTypeIR) {
292:                         AMapMapTypeIR mapType = ((AMapMapTypeIR) type);
293:                         STypeIR from = mapType.getFrom();
294:                         STypeIR to = mapType.getTo();
295:
296:                         // Injective maps are not accounted for
297:•                        if (from instanceof AUnknownTypeIR && to instanceof AUnknownTypeIR) {
298:                                 name = MAP_ANYTHING_TO_ANYTHING;
299:                         } else {
300:                                 issueUnsupportedWarning(methodInst);
301:                                 name = getUnsupportedTypeFieldName();
302:                         }
303:                 }
304:•                else if(type instanceof AUnknownTypeIR)
305:                 {
306:                  name = UNKNOWN;
307:                 }
308:                 else
309:                 {
310:                  issueUnsupportedWarning(methodInst);
311:                  name = getUnsupportedTypeFieldName();
312:                 }
313:                 typeArg.setName(name);
314:                 return typeArg;
315:         }
316:
317: public AExplicitVarExpIR consTypeArg(String className) {
318:
319: AExternalTypeIR runtimeUtilClass = new AExternalTypeIR();
320: runtimeUtilClass.setName(className);
321:
322: AExternalTypeIR anyType = new AExternalTypeIR();
323: anyType.setName(getTypeArgumentFieldName());
324:
325: AExplicitVarExpIR typeArg = new AExplicitVarExpIR();
326: typeArg.setClassType(runtimeUtilClass);
327: typeArg.setIsLocal(false);
328: typeArg.setIsLambda(false);
329: typeArg.setType(anyType);
330: return typeArg;
331: }
332:
333: public void issueUnsupportedWarning(AMethodInstantiationExpIR methodInst) {
334: assist.getInfo().addTransformationWarning(methodInst, "Function instantiation only " +
335: "works for basic types, set of ?, seq of ?, map ? to ?, quotes, unions " +
336:                 "of non-collection types, strings, polymorphic types and records");
337: }
338:
339: public String getUnsupportedTypeFieldName() {
340: return TYPE_NOT_SUPPORTED;
341: }
342:
343: public String getTypeArgumentFieldName() {
344: return OBJECT;
345: }
346:
347: public String getUtilClass() {
348: return UTIL_CLASS;
349: }
350:
351: public String getSetUtil(){
352: return SET_UTIL;
353: }
354:
355: public String getVdmSet()
356: {
357: return VDM_SET;
358: }
359:
360: public String getSetMethodName(){
361: return SET_METHOD_NAME;
362: }
363: }