Package: AFunctionTypeAssistantTC

AFunctionTypeAssistantTC

nameinstructionbranchcomplexitylinemethod
AFunctionTypeAssistantTC(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%
getCurriedPostType(AFunctionType, Boolean)
M: 0 C: 38
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getCurriedPreType(AFunctionType, Boolean)
M: 0 C: 40
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getMeasureType(AFunctionType, boolean, PType, String)
M: 90 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
getPostType(AFunctionType)
M: 0 C: 37
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
getPreType(AFunctionType)
M: 0 C: 23
100%
M: 0 C: 0
100%
M: 0 C: 1
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.type;
23:
24: import java.util.List;
25: import java.util.Vector;
26:
27: import org.overture.ast.assistant.IAstAssistant;
28: import org.overture.ast.assistant.pattern.PTypeList;
29: import org.overture.ast.definitions.PDefinition;
30: import org.overture.ast.factory.AstFactory;
31: import org.overture.ast.types.AFunctionType;
32: import org.overture.ast.types.AProductType;
33: import org.overture.ast.types.PType;
34: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
35:
36: public class AFunctionTypeAssistantTC implements IAstAssistant
37: {
38:         protected ITypeCheckerAssistantFactory af;
39:
40:         public AFunctionTypeAssistantTC(ITypeCheckerAssistantFactory af)
41:         {
42:                 this.af = af;
43:         }
44:
45:         @SuppressWarnings("unchecked")
46:         public AFunctionType getCurriedPreType(AFunctionType t, Boolean isCurried)
47:         {
48:
49:•                if (isCurried && t.getResult() instanceof AFunctionType)
50:                 {
51:                         AFunctionType ft = (AFunctionType) t.getResult().clone();
52:                         AFunctionType type = AstFactory.newAFunctionType(t.getLocation(), false, (List<PType>) t.getParameters().clone(), getCurriedPreType(ft, isCurried));
53:                         type.setDefinitions((List<PDefinition>) t.getDefinitions().clone());
54:                         type.setInstantiated(null);
55:                         return type;
56:                 } else
57:                 {
58:                         return getPreType(t);
59:                 }
60:         }
61:
62:         @SuppressWarnings("unchecked")
63:         public AFunctionType getPreType(AFunctionType t)
64:         {
65:                 AFunctionType type = AstFactory.newAFunctionType(t.getLocation(), false, (List<PType>) t.getParameters().clone(), AstFactory.newABooleanBasicType(t.getLocation()));
66:                 type.setDefinitions((List<PDefinition>) t.getDefinitions().clone());
67:                 type.setInstantiated(null);
68:                 return type;
69:         }
70:
71:         @SuppressWarnings("unchecked")
72:         public AFunctionType getCurriedPostType(AFunctionType type,
73:                         Boolean isCurried)
74:         {
75:•                if (isCurried && type.getResult() instanceof AFunctionType)
76:                 {
77:                         AFunctionType ft = (AFunctionType) type.getResult().clone();
78:                         AFunctionType t = AstFactory.newAFunctionType(type.getLocation(), false, ((List<PType>) type.getParameters().clone()), getCurriedPostType(ft, isCurried));
79:                         t.setDefinitions(type.getDefinitions());
80:                         t.setInstantiated(null);
81:                         return t;
82:                 } else
83:                 {
84:                         return getPostType(type);
85:                 }
86:         }
87:
88:         @SuppressWarnings("unchecked")
89:         public AFunctionType getPostType(AFunctionType t)
90:         {
91:                 List<PType> params = new PTypeList();
92:                 params.addAll((List<PType>) t.getParameters().clone());
93:                 params.add(t.getResult().clone());
94:                 AFunctionType type = AstFactory.newAFunctionType(t.getLocation(), false, params, AstFactory.newABooleanBasicType(t.getLocation()));
95:                 type.setDefinitions((List<? extends PDefinition>) t.getDefinitions().clone());
96:                 type.setInstantiated(null);
97:                 return type;
98:         }
99:
100:         public AFunctionType getMeasureType(AFunctionType type, boolean isCurried, PType actual, String fromModule)
101:         {
102:                 List<PType> cparams = new Vector<PType>();
103:                 cparams.addAll(type.getParameters());
104:                 
105:•                if (isCurried)
106:                 {
107:                         AFunctionType ft = type;
108:                         
109:•                        while (ft.getResult() instanceof AFunctionType)
110:                         {
111:                                 ft = (AFunctionType)type.getResult();
112:                                 cparams.addAll(ft.getParameters());
113:                         }
114:                 }
115:                 
116:                 // Clean the return types to be precisely nat or nat-tuple.
117:                 
118:•                if (af.createPTypeAssistant().isNumeric(actual, fromModule))
119:                 {
120:                         actual = AstFactory.newANatNumericBasicType(type.getLocation());
121:                 }
122:•                else if (af.createPTypeAssistant().isProduct(actual, fromModule))
123:                 {
124:                         AProductType p = af.createPTypeAssistant().getProduct(actual, fromModule);
125:                         List<PType> nats = new Vector<PType>();
126:                         
127:•                        for (int i=0; i<p.getTypes().size(); i++)
128:                         {
129:                                 nats.add(AstFactory.newANatNumericBasicType(type.getLocation()));
130:                         }
131:                         
132:                         actual = AstFactory.newAProductType(type.getLocation(), nats);
133:                 }
134:
135:                 AFunctionType mtype = AstFactory.newAFunctionType(type.getLocation(), false, cparams, actual);
136:                 mtype.setDefinitions(type.getDefinitions());
137:                 return mtype;
138:         }
139:
140: }