Package: TypeCheckerExportsVisitor

TypeCheckerExportsVisitor

nameinstructionbranchcomplexitylinemethod
TypeCheckerExportsVisitor(IQuestionAnswer)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
caseAAllExport(AAllExport, TypeCheckInfo)
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%
caseAFunctionExport(AFunctionExport, TypeCheckInfo)
M: 32 C: 293
90%
M: 7 C: 25
78%
M: 7 C: 10
59%
M: 2 C: 47
96%
M: 0 C: 1
100%
caseAOperationExport(AOperationExport, TypeCheckInfo)
M: 27 C: 56
67%
M: 3 C: 5
63%
M: 3 C: 2
40%
M: 4 C: 11
73%
M: 0 C: 1
100%
caseATypeExport(ATypeExport, TypeCheckInfo)
M: 31 C: 38
55%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 4 C: 9
69%
M: 0 C: 1
100%
caseAValueExport(AValueExport, TypeCheckInfo)
M: 12 C: 85
88%
M: 3 C: 7
70%
M: 3 C: 3
50%
M: 2 C: 14
88%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * The VDM Type Checker
4: * %%
5: * Copyright (C) 2008 - 2016 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.visitor;
23:
24: import org.overture.ast.analysis.AnalysisException;
25: import org.overture.ast.analysis.intf.IQuestionAnswer;
26: import org.overture.ast.definitions.AExplicitFunctionDefinition;
27: import org.overture.ast.definitions.AImplicitFunctionDefinition;
28: import org.overture.ast.definitions.AUntypedDefinition;
29: import org.overture.ast.definitions.PDefinition;
30: import org.overture.ast.intf.lex.ILexNameToken;
31: import org.overture.ast.modules.AAllExport;
32: import org.overture.ast.modules.AFunctionExport;
33: import org.overture.ast.modules.AOperationExport;
34: import org.overture.ast.modules.ATypeExport;
35: import org.overture.ast.modules.AValueExport;
36: import org.overture.ast.typechecker.NameScope;
37: import org.overture.ast.types.ANamedInvariantType;
38: import org.overture.ast.types.ARecordInvariantType;
39: import org.overture.ast.types.PType;
40: import org.overture.typechecker.FlatCheckedEnvironment;
41: import org.overture.typechecker.ModuleEnvironment;
42: import org.overture.typechecker.TypeCheckInfo;
43: import org.overture.typechecker.TypeCheckerErrors;
44: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
45:
46: public class TypeCheckerExportsVisitor extends AbstractTypeCheckVisitor
47: {
48:         public TypeCheckerExportsVisitor(IQuestionAnswer<TypeCheckInfo, PType> typeCheckVisitor)
49:         {
50:                 super(typeCheckVisitor);
51:         }
52:         
53:         public PType caseAAllExport(AAllExport node, TypeCheckInfo question) throws AnalysisException
54:         {
55:                 return null; // Implicitly OK.
56:         }
57:
58:         public PType caseAFunctionExport(AFunctionExport exp, TypeCheckInfo question) throws AnalysisException
59:         {
60:                 ITypeCheckerAssistantFactory af = question.assistantFactory;
61:                 ModuleEnvironment menv = (ModuleEnvironment)question.env;
62:                 
63:•                for (ILexNameToken name : exp.getNameList())
64:                 {
65:                         PDefinition def = af.createPDefinitionListAssistant().findName(menv.getDefinitions(), name, NameScope.NAMES);
66:
67:•                        if (def == null)
68:                         {
69:                                 TypeCheckerErrors.report(3183, "Exported function " + name
70:                                                 + " not defined in module", name.getLocation(), exp);
71:                         }
72:                         else
73:                         {
74:                                 PType act = af.createPDefinitionAssistant().getType(def);
75:•                                if (exp.getTypeParams() != null && !exp.getTypeParams().isEmpty())
76:                                 {
77:•                                        if (def instanceof AExplicitFunctionDefinition)
78:                                         {
79:                                                 AExplicitFunctionDefinition efd = (AExplicitFunctionDefinition)def;
80:                                                 FlatCheckedEnvironment params = new FlatCheckedEnvironment(af, af.createAExplicitFunctionDefinitionAssistant().getTypeParamDefinitions(efd), question.env, NameScope.NAMES);
81:                                                 TypeCheckInfo newQuestion = question.newInfo(params);
82:                                                 PType type = question.assistantFactory.createPTypeAssistant().typeResolve(exp.getExportType(), null, THIS, newQuestion);
83:                                                 exp.setExportType(type);
84:                                         
85:•                                                if (efd.getTypeParams() == null)
86:                                                 {
87:                                                         TypeCheckerErrors.report(3352, "Exported " + name + " function has no type paramaters", name.getLocation(), exp);
88:                                                 }
89:•                                                else if (!efd.getTypeParams().equals(exp.getTypeParams()))
90:                                                 {
91:                                                         TypeCheckerErrors.report(3353, "Exported " + name + " function type parameters incorrect", name.getLocation(), exp);
92:                                                         TypeCheckerErrors.detail2("Exported", exp.getTypeParams(), "Actual", efd.getTypeParams());
93:                                                 }
94:                                                 
95:•                                                if (act != null && !act.toString().equals(type.toString()))
96:                                                 {
97:                                                         TypeCheckerErrors.report(3184, "Exported " + name
98:                                                                         + " function type incorrect", name.getLocation(), exp);
99:                                                         TypeCheckerErrors.detail2("Exported", type, "Actual", act);
100:                                                 }
101:                                         }
102:•                                        else if (def instanceof AImplicitFunctionDefinition)
103:                                         {
104:                                                 AImplicitFunctionDefinition ifd = (AImplicitFunctionDefinition)def;
105:                                                 FlatCheckedEnvironment params = new FlatCheckedEnvironment(af, af.createAImplicitFunctionDefinitionAssistant().getTypeParamDefinitions(ifd), question.env, NameScope.NAMES);
106:                                                 TypeCheckInfo newQuestion = question.newInfo(params);
107:                                                 PType type = question.assistantFactory.createPTypeAssistant().typeResolve(exp.getExportType(), null, THIS, newQuestion);
108:                                                 exp.setExportType(type);
109:                                                 
110:•                                                if (ifd.getTypeParams() == null)
111:                                                 {
112:                                                         TypeCheckerErrors.report(3352, "Exported " + name + " function has no type paramaters", name.getLocation(), exp);
113:                                                 }
114:•                                                else if (!ifd.getTypeParams().equals(exp.getTypeParams()))
115:                                                 {
116:                                                         TypeCheckerErrors.report(3353, "Exported " + name + " function type parameters incorrect", name.getLocation(), exp);
117:                                                         TypeCheckerErrors.detail2("Exported", exp.getTypeParams(), "Actual", ifd.getTypeParams());
118:                                                 }
119:                                                 
120:•                                                if (act != null && !act.toString().equals(type.toString()))
121:                                                 {
122:                                                         TypeCheckerErrors.report(3184, "Exported " + name
123:                                                                         + " function type incorrect", name.getLocation(), exp);
124:                                                         TypeCheckerErrors.detail2("Exported", type, "Actual", act);
125:                                                 }
126:                                         }
127:                                 }
128:                                 else
129:                                 {
130:                                         PType type = question.assistantFactory.createPTypeAssistant().typeResolve(exp.getExportType(), null, THIS, question);
131:                                         exp.setExportType(type);
132:
133:•                                        if (act != null && !af.createPTypeAssistant().equals(act, type))
134:                                 {
135:                                         TypeCheckerErrors.report(3184, "Exported " + name
136:                                                         + " function type incorrect", name.getLocation(), exp);
137:                                         TypeCheckerErrors.detail2("Exported", type, "Actual", act);
138:                                 }
139:                                 }
140:                         }
141:                 }
142:                 
143:                 return null;
144:         }
145:
146:         public PType caseAOperationExport(AOperationExport exp, TypeCheckInfo question) throws AnalysisException
147:         {
148:                 ITypeCheckerAssistantFactory af = question.assistantFactory;
149:                 ModuleEnvironment menv = (ModuleEnvironment)question.env;
150:
151:•                for (ILexNameToken name : exp.getNameList())
152:                 {
153:                         PDefinition def = af.createPDefinitionListAssistant().findName(menv.getDefinitions(), name, NameScope.NAMES);
154:
155:•                        if (def == null)
156:                         {
157:                                 TypeCheckerErrors.report(3185, "Exported operation " + name
158:                                                 + " not defined in module", name.getLocation(), exp);
159:                         }
160:                         else
161:                         {
162:                                 PType act = def.getType();
163:                                 PType type = question.assistantFactory.createPTypeAssistant().typeResolve(exp.getExportType(), null, THIS, question);
164:                                 exp.setExportType(type);
165:                                 
166:•                                if (act != null && !af.createPTypeAssistant().equals(act, type))
167:                                 {
168:                                         TypeCheckerErrors.report(3186, "Exported operation type does not match actual type", name.getLocation(), exp);
169:                                         TypeCheckerErrors.detail2("Exported", type, "Actual", act);
170:                                 }
171:                         }
172:                 }
173:                 
174:                 return null;
175:         }
176:
177:         public PType caseATypeExport(ATypeExport exp, TypeCheckInfo question) throws AnalysisException
178:         {
179:                 ILexNameToken name = exp.getName();
180:                 ITypeCheckerAssistantFactory af = question.assistantFactory;
181:                 ModuleEnvironment menv = (ModuleEnvironment)question.env;
182:                 
183:                 PDefinition def = af.createPDefinitionListAssistant().findType(menv.getDefinitions(), name, name.getModule());
184:                 
185:•                if (def == null)
186:                 {
187:                         TypeCheckerErrors.report(3187, "Exported type " + name
188:                                         + " not defined in module", name.getLocation(), exp);
189:                 }
190:                 else
191:                 {
192:•                        if (exp.getStruct())
193:                         {
194:                                 PType type = af.createPDefinitionAssistant().getType(def);
195:
196:•                                if (!(type instanceof ANamedInvariantType) && !(type instanceof ARecordInvariantType))
197:                                 {
198:                                         TypeCheckerErrors.report(67, "Exported type " + name
199:                                                         + " not structured", name.getLocation(), exp);
200:                                 }
201:                         }
202:                 }
203:                 
204:                 return null;
205:         }
206:
207:         public PType caseAValueExport(AValueExport exp, TypeCheckInfo question) throws AnalysisException
208:         {
209:                 ITypeCheckerAssistantFactory af = question.assistantFactory;
210:                 ModuleEnvironment menv = (ModuleEnvironment)question.env;
211:                 PType type = question.assistantFactory.createPTypeAssistant().typeResolve(exp.getExportType().clone(), null, THIS, question);
212:                 exp.setExportType(type);
213:
214:•                for (ILexNameToken name : exp.getNameList())
215:                 {
216:                         PDefinition def = af.createPDefinitionListAssistant().findName(menv.getDefinitions(), name, NameScope.NAMES);
217:
218:•                        if (def == null)
219:                         {
220:                                 TypeCheckerErrors.report(3188, "Exported value " + name
221:                                                 + " not defined in module", name.getLocation(), exp);
222:                         }
223:•                        else if (def instanceof AUntypedDefinition)
224:                         {
225:                                 // OK
226:                         }
227:                         else
228:                         {
229:                                 PType act = question.assistantFactory.createPTypeAssistant().typeResolve(def.getType(), null, THIS, question);
230:
231:•                                if (act != null && !question.assistantFactory.getTypeComparator().compatible(act, type))
232:                                 {
233:                                         TypeCheckerErrors.report(3189, "Exported type does not match actual type", act.getLocation(), act);
234:                                         TypeCheckerErrors.detail2("Exported", type, "Actual", act);
235:                                 }
236:                         }
237:                 }
238:
239:                 return null;
240:         }
241: }