Package: AExplicitFunctionDefinitionAssistantTC

AExplicitFunctionDefinitionAssistantTC

nameinstructionbranchcomplexitylinemethod
AExplicitFunctionDefinitionAssistantTC(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%
checkParams(AExplicitFunctionDefinition, ListIterator, AFunctionType)
M: 13 C: 59
82%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 3 C: 14
82%
M: 0 C: 1
100%
getMeasureParams(AExplicitFunctionDefinition)
M: 18 C: 18
50%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 6 C: 5
45%
M: 0 C: 1
100%
getPostDefinition(AExplicitFunctionDefinition)
M: 0 C: 137
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 21
100%
M: 0 C: 1
100%
getPreDefinition(AExplicitFunctionDefinition)
M: 0 C: 42
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getType(AExplicitFunctionDefinition, List)
M: 0 C: 40
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 9
100%
M: 0 C: 1
100%
getTypeParamDefinitions(AExplicitFunctionDefinition)
M: 0 C: 37
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
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.definition;
23:
24: import java.util.ArrayList;
25: import java.util.Iterator;
26: import java.util.LinkedList;
27: import java.util.List;
28: import java.util.ListIterator;
29: import java.util.Vector;
30:
31: import org.overture.ast.assistant.IAstAssistant;
32: import org.overture.ast.definitions.AExplicitFunctionDefinition;
33: import org.overture.ast.definitions.PDefinition;
34: import org.overture.ast.factory.AstFactory;
35: import org.overture.ast.intf.lex.ILexNameToken;
36: import org.overture.ast.lex.LexNameToken;
37: import org.overture.ast.node.NodeList;
38: import org.overture.ast.patterns.PPattern;
39: import org.overture.ast.typechecker.NameScope;
40: import org.overture.ast.types.AFunctionType;
41: import org.overture.ast.types.PType;
42: import org.overture.typechecker.TypeChecker;
43: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
44:
45: public class AExplicitFunctionDefinitionAssistantTC implements IAstAssistant
46: {
47:         protected ITypeCheckerAssistantFactory af;
48:
49:         public AExplicitFunctionDefinitionAssistantTC(
50:                         ITypeCheckerAssistantFactory af)
51:         {
52:                 this.af = af;
53:         }
54:
55:         public List<PType> getMeasureParams(AExplicitFunctionDefinition node)
56:         {
57:                 AFunctionType functionType = (AFunctionType) node.getType();
58:
59:                 List<PType> params = new LinkedList<PType>();
60:                 params.addAll(functionType.getParameters());
61:
62:•                if (node.getIsCurried())
63:                 {
64:                         PType rtype = functionType.getResult();
65:
66:•                        while (rtype instanceof AFunctionType)
67:                         {
68:                                 AFunctionType ftype = (AFunctionType) rtype;
69:                                 params.addAll(ftype.getParameters());
70:                                 rtype = ftype.getResult();
71:                         }
72:                 }
73:
74:                 return params;
75:         }
76:
77:         public PType checkParams(AExplicitFunctionDefinition node,
78:                         ListIterator<List<PPattern>> plists, AFunctionType ftype)
79:         {
80:                 List<PType> ptypes = ftype.getParameters();
81:                 List<PPattern> patterns = plists.next();
82:
83:•                if (patterns.size() > ptypes.size())
84:                 {
85:                         TypeChecker.report(3020, "Too many parameter patterns", node.getLocation());
86:                         TypeChecker.detail2("Pattern(s)", patterns, "Type(s)", ptypes);
87:                         return ftype.getResult();
88:•                } else if (patterns.size() < ptypes.size())
89:                 {
90:                         TypeChecker.report(3021, "Too few parameter patterns", node.getLocation());
91:                         TypeChecker.detail2("Pattern(s)", patterns, "Type(s)", ptypes);
92:                         return ftype.getResult();
93:                 }
94:
95:•                if (ftype.getResult() instanceof AFunctionType)
96:                 {
97:•                        if (!plists.hasNext())
98:                         {
99:                                 // We're returning the function itself
100:                                 return ftype.getResult();
101:                         }
102:
103:                         // We're returning what the function returns, assuming we
104:                         // pass the right parameters. Note that this recursion
105:                         // means that we finally return the result of calling the
106:                         // function with *all* of the curried argument sets applied.
107:                         // This is because the type check of the body determines
108:                         // the return type when all of the curried parameters are
109:                         // provided.
110:
111:                         return checkParams(node, plists, (AFunctionType) ftype.getResult());
112:                 }
113:
114:•                if (plists.hasNext())
115:                 {
116:                         TypeChecker.report(3022, "Too many curried parameters", node.getLocation());
117:                 }
118:
119:                 return ftype.getResult();
120:         }
121:
122:         public List<PDefinition> getTypeParamDefinitions(
123:                         AExplicitFunctionDefinition node)
124:         {
125:                 List<PDefinition> defs = new ArrayList<PDefinition>();
126:
127:•                for (ILexNameToken pname : node.getTypeParams())
128:                 {
129:                         PDefinition p = AstFactory.newALocalDefinition(pname.getLocation(), pname.clone(), NameScope.NAMES, AstFactory.newAParameterType(pname.clone()));
130:                         // pname.location, NameScope.NAMES,false,null, null, new
131:                         // AParameterType(null,false,null,pname.clone()),false,pname.clone());
132:
133:                         af.createPDefinitionAssistant().markUsed(p);
134:                         defs.add(p);
135:                 }
136:
137:                 return defs;
138:         }
139:
140:         public AFunctionType getType(AExplicitFunctionDefinition efd,
141:                         List<PType> actualTypes)
142:         {
143:                 Iterator<PType> ti = actualTypes.iterator();
144:                 AFunctionType ftype = (AFunctionType) efd.getType();
145:
146:•                if (efd.getTypeParams() != null)
147:                 {
148:•                        for (ILexNameToken pname : efd.getTypeParams())
149:                         {
150:                                 PType ptype = ti.next();
151:                                 ftype = (AFunctionType) af.createPTypeAssistant().polymorph(ftype, pname, ptype);
152:                         }
153:                         
154:                         ftype.setInstantiated(true);
155:                 }
156:
157:                 return ftype;
158:         }
159:
160:         public AExplicitFunctionDefinition getPostDefinition(
161:                         AExplicitFunctionDefinition d)
162:         {
163:
164:                 List<PPattern> last = new Vector<PPattern>();
165:                 int psize = d.getParamPatternList().size();
166:
167:•                for (PPattern p : d.getParamPatternList().get(psize - 1))
168:                 {
169:                         last.add(p.clone());
170:                 }
171:
172:                 LexNameToken result = new LexNameToken(d.getName().getModule(), "RESULT", d.getLocation());
173:                 last.add(AstFactory.newAIdentifierPattern(result));
174:
175:                 List<List<PPattern>> parameters = new Vector<List<PPattern>>();
176:
177:•                if (psize > 1)
178:                 {
179:
180:•                        for (List<PPattern> pPatternList : d.getParamPatternList().subList(0, psize - 1))
181:                         {
182:                                 NodeList<PPattern> tmpList = new NodeList<PPattern>(null);
183:•                                for (PPattern pPattern2 : pPatternList)
184:                                 {
185:                                         tmpList.add(pPattern2.clone());
186:                                 }
187:                                 parameters.add(tmpList);
188:                         }
189:                         // parameters.addAll(d.getParamPatternList().subList(0, psize - 1));
190:                 }
191:
192:                 parameters.add(last);
193:
194:                 @SuppressWarnings("unchecked")
195:                 AExplicitFunctionDefinition def = AstFactory.newAExplicitFunctionDefinition(d.getName().getPostName(d.getPostcondition().getLocation()), NameScope.GLOBAL, (List<ILexNameToken>) d.getTypeParams().clone(), af.createAFunctionTypeAssistant().getCurriedPostType((AFunctionType) d.getType(), d.getIsCurried()), parameters, d.getPostcondition(), null, null, false, null);
196:
197:                 def.setAccess(d.getAccess().clone());
198:                 def.setClassDefinition(d.getClassDefinition());
199:                 return def;
200:         }
201:
202:         public AExplicitFunctionDefinition getPreDefinition(
203:                         AExplicitFunctionDefinition d)
204:         {
205:
206:                 @SuppressWarnings("unchecked")
207:                 AExplicitFunctionDefinition def = AstFactory.newAExplicitFunctionDefinition(d.getName().getPreName(d.getPrecondition().getLocation()), NameScope.GLOBAL, (List<ILexNameToken>) d.getTypeParams().clone(), af.createAFunctionTypeAssistant().getCurriedPreType((AFunctionType) d.getType(), d.getIsCurried()), (LinkedList<List<PPattern>>) d.getParamPatternList().clone(), d.getPrecondition(), null, null, false, null);
208:
209:                 def.setAccess(d.getAccess().clone());
210:                 def.setClassDefinition(d.getClassDefinition());
211:
212:                 return def;
213:         }
214:
215: }