OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013, the Dart project authors. | 2 * Copyright (c) 2013, the Dart project authors. |
3 * | 3 * |
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except | 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u
se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.eclipse.org/legal/epl-v10.html | 7 * http://www.eclipse.org/legal/epl-v10.html |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut
ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K
IND, either express |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 | 145 |
146 /** | 146 /** |
147 * This is set to {@code true} iff the visitor is currently visiting code in t
he SDK. | 147 * This is set to {@code true} iff the visitor is currently visiting code in t
he SDK. |
148 */ | 148 */ |
149 private boolean isInSystemLibrary; | 149 private boolean isInSystemLibrary; |
150 | 150 |
151 /** | 151 /** |
152 * The method or function that we are currently visiting, or {@code null} if w
e are not inside a | 152 * The method or function that we are currently visiting, or {@code null} if w
e are not inside a |
153 * method or function. | 153 * method or function. |
154 */ | 154 */ |
155 private ExecutableElement currentFunction; | 155 private ExecutableElement enclosingFunction; |
156 | 156 |
157 /** | 157 /** |
158 * This map is initialized when visiting the contents of a class declaration.
If the visitor is | 158 * This map is initialized when visiting the contents of a class declaration.
If the visitor is |
159 * not in an enclosing class declaration, then the map is set to {@code null}. | 159 * not in an enclosing class declaration, then the map is set to {@code null}. |
160 * <p> | 160 * <p> |
161 * When set the map maps the set of {@link FieldElement}s in the class to an | 161 * When set the map maps the set of {@link FieldElement}s in the class to an |
162 * {@link INIT_STATE#NOT_INIT} or {@link INIT_STATE#INIT_IN_DECLARATION}. <cod
e>checkFor*</code> | 162 * {@link INIT_STATE#NOT_INIT} or {@link INIT_STATE#INIT_IN_DECLARATION}. <cod
e>checkFor*</code> |
163 * methods, specifically {@link #checkForAllFinalInitializedErrorCodes(Constru
ctorDeclaration)}, | 163 * methods, specifically {@link #checkForAllFinalInitializedErrorCodes(Constru
ctorDeclaration)}, |
164 * can make a copy of the map to compute error code states. <code>checkFor*</c
ode> methods should | 164 * can make a copy of the map to compute error code states. <code>checkFor*</c
ode> methods should |
165 * only ever make a copy, or read from this map after it has been set in | 165 * only ever make a copy, or read from this map after it has been set in |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 } | 253 } |
254 | 254 |
255 @Override | 255 @Override |
256 public Void visitConditionalExpression(ConditionalExpression node) { | 256 public Void visitConditionalExpression(ConditionalExpression node) { |
257 checkForNonBoolCondition(node.getCondition()); | 257 checkForNonBoolCondition(node.getCondition()); |
258 return super.visitConditionalExpression(node); | 258 return super.visitConditionalExpression(node); |
259 } | 259 } |
260 | 260 |
261 @Override | 261 @Override |
262 public Void visitConstructorDeclaration(ConstructorDeclaration node) { | 262 public Void visitConstructorDeclaration(ConstructorDeclaration node) { |
263 ExecutableElement previousFunction = currentFunction; | 263 ExecutableElement outerFunction = enclosingFunction; |
264 try { | 264 try { |
265 currentFunction = node.getElement(); | 265 enclosingFunction = node.getElement(); |
266 isEnclosingConstructorConst = node.getConstKeyword() != null; | 266 isEnclosingConstructorConst = node.getConstKeyword() != null; |
267 checkForConstConstructorWithNonFinalField(node); | 267 checkForConstConstructorWithNonFinalField(node); |
268 checkForConflictingConstructorNameAndMember(node); | 268 checkForConflictingConstructorNameAndMember(node); |
269 checkForAllFinalInitializedErrorCodes(node); | 269 checkForAllFinalInitializedErrorCodes(node); |
270 return super.visitConstructorDeclaration(node); | 270 return super.visitConstructorDeclaration(node); |
271 } finally { | 271 } finally { |
272 isEnclosingConstructorConst = false; | 272 isEnclosingConstructorConst = false; |
273 currentFunction = previousFunction; | 273 enclosingFunction = outerFunction; |
274 } | 274 } |
275 } | 275 } |
276 | 276 |
277 @Override | 277 @Override |
278 public Void visitDoStatement(DoStatement node) { | 278 public Void visitDoStatement(DoStatement node) { |
279 checkForNonBoolCondition(node.getCondition()); | 279 checkForNonBoolCondition(node.getCondition()); |
280 return super.visitDoStatement(node); | 280 return super.visitDoStatement(node); |
281 } | 281 } |
282 | 282 |
283 @Override | 283 @Override |
284 public Void visitExtendsClause(ExtendsClause node) { | 284 public Void visitExtendsClause(ExtendsClause node) { |
285 checkForExtendsDisallowedClass(node); | 285 checkForExtendsDisallowedClass(node); |
286 return super.visitExtendsClause(node); | 286 return super.visitExtendsClause(node); |
287 } | 287 } |
288 | 288 |
289 @Override | 289 @Override |
290 public Void visitFieldFormalParameter(FieldFormalParameter node) { | 290 public Void visitFieldFormalParameter(FieldFormalParameter node) { |
291 checkForConstFormalParameter(node); | 291 checkForConstFormalParameter(node); |
292 checkForFieldInitializerOutsideConstructor(node); | 292 checkForFieldInitializerOutsideConstructor(node); |
293 return super.visitFieldFormalParameter(node); | 293 return super.visitFieldFormalParameter(node); |
294 } | 294 } |
295 | 295 |
296 @Override | 296 @Override |
297 public Void visitFunctionDeclaration(FunctionDeclaration node) { | 297 public Void visitFunctionDeclaration(FunctionDeclaration node) { |
298 ExecutableElement previousFunction = currentFunction; | 298 ExecutableElement outerFunction = enclosingFunction; |
299 try { | 299 try { |
300 currentFunction = node.getElement(); | 300 enclosingFunction = node.getElement(); |
301 return super.visitFunctionDeclaration(node); | 301 return super.visitFunctionDeclaration(node); |
302 } finally { | 302 } finally { |
303 currentFunction = previousFunction; | 303 enclosingFunction = outerFunction; |
304 } | 304 } |
305 } | 305 } |
306 | 306 |
307 @Override | 307 @Override |
308 public Void visitFunctionExpression(FunctionExpression node) { | 308 public Void visitFunctionExpression(FunctionExpression node) { |
309 ExecutableElement previousFunction = currentFunction; | 309 ExecutableElement outerFunction = enclosingFunction; |
310 try { | 310 try { |
311 currentFunction = node.getElement(); | 311 enclosingFunction = node.getElement(); |
312 return super.visitFunctionExpression(node); | 312 return super.visitFunctionExpression(node); |
313 } finally { | 313 } finally { |
314 currentFunction = previousFunction; | 314 enclosingFunction = outerFunction; |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 @Override | 318 @Override |
319 public Void visitFunctionTypeAlias(FunctionTypeAlias node) { | 319 public Void visitFunctionTypeAlias(FunctionTypeAlias node) { |
320 checkForBuiltInIdentifierAsName( | 320 checkForBuiltInIdentifierAsName( |
321 node.getName(), | 321 node.getName(), |
322 CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME); | 322 CompileTimeErrorCode.BUILT_IN_IDENTIFIER_AS_TYPEDEF_NAME); |
323 checkForDefaultValueInFunctionTypeAlias(node); | 323 checkForDefaultValueInFunctionTypeAlias(node); |
324 return super.visitFunctionTypeAlias(node); | 324 return super.visitFunctionTypeAlias(node); |
(...skipping 23 matching lines...) Expand all Loading... |
348 // TODO(jwren) Email Luke to make this determination: Should we always cal
l all checks, if not, | 348 // TODO(jwren) Email Luke to make this determination: Should we always cal
l all checks, if not, |
349 // which order should they be called in? | 349 // which order should they be called in? |
350 // (Should we provide as many errors as possible, or try to be as concise
as possible?) | 350 // (Should we provide as many errors as possible, or try to be as concise
as possible?) |
351 checkForTypeArgumentNotMatchingBounds(node, constructorName.getElement(),
typeName); | 351 checkForTypeArgumentNotMatchingBounds(node, constructorName.getElement(),
typeName); |
352 } | 352 } |
353 return super.visitInstanceCreationExpression(node); | 353 return super.visitInstanceCreationExpression(node); |
354 } | 354 } |
355 | 355 |
356 @Override | 356 @Override |
357 public Void visitMethodDeclaration(MethodDeclaration node) { | 357 public Void visitMethodDeclaration(MethodDeclaration node) { |
358 ExecutableElement previousFunction = currentFunction; | 358 ExecutableElement previousFunction = enclosingFunction; |
359 try { | 359 try { |
360 currentFunction = node.getElement(); | 360 enclosingFunction = node.getElement(); |
361 return super.visitMethodDeclaration(node); | 361 return super.visitMethodDeclaration(node); |
362 } finally { | 362 } finally { |
363 currentFunction = previousFunction; | 363 enclosingFunction = previousFunction; |
364 } | 364 } |
365 } | 365 } |
366 | 366 |
367 @Override | 367 @Override |
368 public Void visitNativeFunctionBody(NativeFunctionBody node) { | 368 public Void visitNativeFunctionBody(NativeFunctionBody node) { |
369 checkForNativeFunctionBodyInNonSDKCode(node); | 369 checkForNativeFunctionBodyInNonSDKCode(node); |
370 return super.visitNativeFunctionBody(node); | 370 return super.visitNativeFunctionBody(node); |
371 } | 371 } |
372 | 372 |
373 @Override | 373 @Override |
(...skipping 697 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1071 | 1071 |
1072 /** | 1072 /** |
1073 * This checks that the return type matches the type of the declared return ty
pe in the enclosing | 1073 * This checks that the return type matches the type of the declared return ty
pe in the enclosing |
1074 * method or function. | 1074 * method or function. |
1075 * | 1075 * |
1076 * @param node the return statement to evaluate | 1076 * @param node the return statement to evaluate |
1077 * @return return {@code true} if and only if an error code is generated on th
e passed node | 1077 * @return return {@code true} if and only if an error code is generated on th
e passed node |
1078 * @see StaticTypeWarningCode#RETURN_OF_INVALID_TYPE | 1078 * @see StaticTypeWarningCode#RETURN_OF_INVALID_TYPE |
1079 */ | 1079 */ |
1080 private boolean checkForReturnOfInvalidType(ReturnStatement node) { | 1080 private boolean checkForReturnOfInvalidType(ReturnStatement node) { |
1081 FunctionType functionType = currentFunction == null ? null : currentFunction
.getType(); | 1081 FunctionType functionType = enclosingFunction == null ? null : enclosingFunc
tion.getType(); |
1082 Type expectedReturnType = functionType == null ? null : functionType.getRetu
rnType(); | 1082 Type expectedReturnType = functionType == null ? null : functionType.getRetu
rnType(); |
1083 Expression returnExpression = node.getExpression(); | 1083 Expression returnExpression = node.getExpression(); |
1084 if (expectedReturnType != null && !expectedReturnType.isVoid() && returnExpr
ession != null) { | 1084 if (expectedReturnType != null && !expectedReturnType.isVoid() && returnExpr
ession != null) { |
1085 Type actualReturnType = getType(returnExpression); | 1085 Type actualReturnType = getType(returnExpression); |
1086 if (!actualReturnType.isAssignableTo(expectedReturnType)) { | 1086 if (!actualReturnType.isAssignableTo(expectedReturnType)) { |
1087 errorReporter.reportError( | 1087 errorReporter.reportError( |
1088 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE, | 1088 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE, |
1089 returnExpression, | 1089 returnExpression, |
1090 actualReturnType.getName(), | 1090 actualReturnType.getName(), |
1091 expectedReturnType.getName(), | 1091 expectedReturnType.getName(), |
1092 currentFunction.getName()); | 1092 enclosingFunction.getName()); |
1093 return true; | 1093 return true; |
1094 } | 1094 } |
1095 } | 1095 } |
1096 return false; | 1096 return false; |
1097 } | 1097 } |
1098 | 1098 |
1099 /** | 1099 /** |
1100 * This verifies that the type arguments in the passed instance creation expre
ssion are all within | 1100 * This verifies that the type arguments in the passed instance creation expre
ssion are all within |
1101 * their bounds as specified by the class element where the constructor [that
is being invoked] is | 1101 * their bounds as specified by the class element where the constructor [that
is being invoked] is |
1102 * declared. | 1102 * declared. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1156 private VariableElement getVariableElement(Expression expression) { | 1156 private VariableElement getVariableElement(Expression expression) { |
1157 if (expression instanceof Identifier) { | 1157 if (expression instanceof Identifier) { |
1158 Element element = ((Identifier) expression).getElement(); | 1158 Element element = ((Identifier) expression).getElement(); |
1159 if (element instanceof VariableElement) { | 1159 if (element instanceof VariableElement) { |
1160 return (VariableElement) element; | 1160 return (VariableElement) element; |
1161 } | 1161 } |
1162 } | 1162 } |
1163 return null; | 1163 return null; |
1164 } | 1164 } |
1165 } | 1165 } |
OLD | NEW |