Package: ClassTypeChecker

ClassTypeChecker

nameinstructionbranchcomplexitylinemethod
ClassTypeChecker(List)
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%
ClassTypeChecker(List, ITypeCheckerAssistantFactory)
M: 0 C: 7
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
getAllClassesEnvronment()
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%
getTypeCheckVisitor()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
typeCheck()
M: 65 C: 348
84%
M: 16 C: 42
72%
M: 13 C: 17
57%
M: 16 C: 67
81%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: *
3: *        Copyright (c) 2008 Fujitsu Services Ltd.
4: *
5: *        Author: Nick Battle
6: *
7: *        This file is part of VDMJ.
8: *
9: *        VDMJ is free software: you can redistribute it and/or modify
10: *        it under the terms of the GNU General Public License as published by
11: *        the Free Software Foundation, either version 3 of the License, or
12: *        (at your option) any later version.
13: *
14: *        VDMJ is distributed in the hope that it will be useful,
15: *        but WITHOUT ANY WARRANTY; without even the implied warranty of
16: *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: *        GNU General Public License for more details.
18: *
19: *        You should have received a copy of the GNU General Public License
20: *        along with VDMJ. If not, see <http://www.gnu.org/licenses/>.
21: *
22: ******************************************************************************/
23:
24: package org.overture.typechecker;
25:
26: import java.util.List;
27: import java.util.Vector;
28:
29: import org.overture.ast.analysis.AnalysisException;
30: import org.overture.ast.analysis.QuestionAnswerAdaptor;
31: import org.overture.ast.annotations.Annotation;
32: import org.overture.ast.annotations.PAnnotation;
33: import org.overture.ast.definitions.ASystemClassDefinition;
34: import org.overture.ast.definitions.PDefinition;
35: import org.overture.ast.definitions.SClassDefinition;
36: import org.overture.ast.typechecker.Pass;
37: import org.overture.ast.types.PType;
38: import org.overture.typechecker.annotations.TCAnnotation;
39: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
40: import org.overture.typechecker.visitor.TypeCheckVisitor;
41:
42: /**
43: * A class to coordinate all class type checking processing.
44: */
45:
46: public class ClassTypeChecker extends TypeChecker
47: {
48:         /** The list of classes to check. */
49:         protected final List<SClassDefinition> classes;
50:
51:         /**
52:          * Create a type checker with the list of classes passed.
53:          *
54:          * @param classes
55:          */
56:
57:         public ClassTypeChecker(List<SClassDefinition> classes)
58:         {
59:                 super();
60:                 this.classes = classes;
61:         }
62:
63:         /**
64:          * Create a type checker with the list of classes passed.
65:          *
66:          * @param classes
67:          * @param factory
68:          */
69:
70:         public ClassTypeChecker(List<SClassDefinition> classes,
71:                         ITypeCheckerAssistantFactory factory)
72:         {
73:                 super(factory);
74:                 this.classes = classes;
75:         }
76:
77:         /**
78:          * Perform type checking across all classes in the list.
79:          */
80:
81:         @Override
82:         public void typeCheck()
83:         {
84:                 boolean nothing = true;
85:                 boolean hasSystem = false;
86:
87:•                for (SClassDefinition c1 : classes)
88:                 {
89:                         c1.setType(assistantFactory.createSClassDefinitionAssistant().getType(c1));
90:•                        for (SClassDefinition c2 : classes)
91:                         {
92:•                                if (c1 != c2 && c1.getName().equals(c2.getName()))
93:                                 {
94:                                         TypeChecker.report(3426, "Class " + c1.getName()
95:                                                         + " duplicates " + c2.getName(), c1.getName().getLocation());
96:                                 }
97:                         }
98:
99:•                        if (!c1.getTypeChecked())
100:                         {
101:                                 nothing = false;
102:                         }
103:
104:•                        if (c1 instanceof ASystemClassDefinition)
105:                         {
106:•                                if (hasSystem)
107:                                 {
108:                                         TypeChecker.report(3294, "Only one system class permitted", c1.getLocation());
109:                                 } else
110:                                 {
111:                                         hasSystem = true;
112:                                 }
113:                         }
114:                 }
115:
116:•                if (nothing)
117:                 {
118:                         return;
119:                 }
120:
121:                 Environment allClasses = getAllClassesEnvronment();
122:
123:•                for (SClassDefinition c : classes)
124:                 {
125:•                        if (!c.getTypeChecked())
126:                         {
127:                                 assistantFactory.createSClassDefinitionAssistant().implicitDefinitions(c, allClasses);
128:                         }
129:                 }
130:
131:•                for (SClassDefinition c : classes)
132:                 {
133:•                        if (!c.getTypeChecked())
134:                         {
135:                                 try
136:                                 {
137:                                         Environment self = new PrivateClassEnvironment(assistantFactory, c, allClasses);
138:                                         assistantFactory.createSClassDefinitionAssistant().typeResolve(c, null, new TypeCheckInfo(assistantFactory, self));
139:                                 } catch (TypeCheckException te)
140:                                 {
141:                                         report(3427, te.getMessage(), te.location);
142:                                         
143:•                                 if (te.extras != null)
144:                                 {
145:•                                         for (TypeCheckException e: te.extras)
146:                                         {
147:                                                 report(3427, e.getMessage(), e.location);
148:                                         }
149:                                 }
150:                                 } catch (AnalysisException te)
151:                                 {
152:                                         report(3431, te.getMessage(), null);// FIXME: internal error
153:                                 }
154:                         }
155:                 }
156:
157:•                for (SClassDefinition c : classes)
158:                 {
159:•                        if (!c.getTypeChecked())
160:                         {
161:                                 assistantFactory.createSClassDefinitionAssistant().checkOver(c);
162:                         }
163:                 }
164:
165:                 // Initialise any annotations
166:                 Annotation.init(TCAnnotation.class);
167:
168:•                for (SClassDefinition c: classes)
169:                 {
170:•                        for (PAnnotation annotation: c.getAnnotations())
171:                         {
172:•                                if (annotation.getImpl() instanceof TCAnnotation)
173:                                 {
174:                                         TCAnnotation impl = (TCAnnotation)annotation.getImpl();
175:                                         impl.tcBefore(c, null);
176:                                 }
177:                         }
178:                 }
179:
180:                 // Prepare to look for recursive loops
181:                 RecursiveLoops.getInstance().reset();
182:
183:                 QuestionAnswerAdaptor<TypeCheckInfo, PType> tc = getTypeCheckVisitor();
184:•                for (Pass pass : Pass.values())
185:                 {
186:•                        for (SClassDefinition c : classes)
187:                         {
188:•                                if (!c.getTypeChecked())
189:                                 {
190:                                         try
191:                                         {
192:                                                 Environment self = new PrivateClassEnvironment(assistantFactory, c, allClasses);
193:                                                 assistantFactory.createSClassDefinitionAssistant().typeCheckPass(c, pass, self, tc);
194:                                         } catch (TypeCheckException te)
195:                                         {
196:                                                 report(3428, te.getMessage(), te.location);
197:                                                 
198:•                                         if (te.extras != null)
199:                                          {
200:•                                                 for (TypeCheckException e: te.extras)
201:                                                  {
202:                                                          report(3428, e.getMessage(), e.location);
203:                                                  }
204:                                          }
205:                                         } catch (AnalysisException te)
206:                                         {
207:                                                 report(3431, te.getMessage(), null);// FIXME: internal error
208:                                         }
209:                                 }
210:                         }
211:                 }
212:
213:                 // Look for recursive loops
214:                 RecursiveLoops.getInstance().typeCheckClasses(classes, assistantFactory);
215:
216:•                for (SClassDefinition c: classes)
217:                 {
218:•                        for (PAnnotation annotation: c.getAnnotations())
219:                         {
220:•                                if (annotation.getImpl() instanceof TCAnnotation)
221:                                 {
222:                                         TCAnnotation impl = (TCAnnotation)annotation.getImpl();
223:                                         impl.tcAfter(c, null);
224:                                 }
225:                         }
226:                 }
227:
228:                 List<PDefinition> allDefs = new Vector<PDefinition>();
229:
230:•                for (SClassDefinition c : classes)
231:                 {
232:•                        if (!c.getTypeChecked())
233:                         {
234:                                 assistantFactory.createSClassDefinitionAssistant().initializedCheck(c);
235:                                 assistantFactory.createPDefinitionAssistant().unusedCheck(c);
236:                                 allDefs.addAll(c.getDefinitions());
237:                         }
238:                 }
239:         
240:         cyclicDependencyCheck(allDefs);
241:         }
242:
243:         protected Environment getAllClassesEnvronment()
244:         {
245:                 return new PublicClassEnvironment(assistantFactory, classes, null);
246:         }
247:
248:         protected QuestionAnswerAdaptor<TypeCheckInfo, PType> getTypeCheckVisitor()
249:         {
250:                 return new TypeCheckVisitor();
251:         }
252: }