OLD | NEW |
---|---|
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 library kernel.checks; | 4 library kernel.checks; |
5 | 5 |
6 import 'ast.dart'; | 6 import 'ast.dart'; |
7 import 'transformations/flags.dart'; | 7 import 'transformations/flags.dart'; |
8 | 8 |
9 void verifyProgram(Program program) { | 9 void verifyProgram(Program program) { |
10 VerifyingVisitor.check(program); | 10 VerifyingVisitor.check(program); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 class VerifyingVisitor extends RecursiveVisitor { | 44 class VerifyingVisitor extends RecursiveVisitor { |
45 final Set<Class> classes = new Set<Class>(); | 45 final Set<Class> classes = new Set<Class>(); |
46 final Set<TypeParameter> typeParameters = new Set<TypeParameter>(); | 46 final Set<TypeParameter> typeParameters = new Set<TypeParameter>(); |
47 final List<VariableDeclaration> variableStack = <VariableDeclaration>[]; | 47 final List<VariableDeclaration> variableStack = <VariableDeclaration>[]; |
48 bool classTypeParametersAreInScope = false; | 48 bool classTypeParametersAreInScope = false; |
49 | 49 |
50 /// If true, relax certain checks for *outline* mode. For example, don't | 50 /// If true, relax certain checks for *outline* mode. For example, don't |
51 /// attempt to validate constructor initializers. | 51 /// attempt to validate constructor initializers. |
52 bool isOutline = false; | 52 bool isOutline = false; |
53 | 53 |
54 bool inCatchBlock = false; | |
55 | |
54 Member currentMember; | 56 Member currentMember; |
55 Class currentClass; | 57 Class currentClass; |
56 TreeNode currentParent; | 58 TreeNode currentParent; |
57 | 59 |
58 TreeNode get context => currentMember ?? currentClass; | 60 TreeNode get context => currentMember ?? currentClass; |
59 | 61 |
60 static void check(Program program) { | 62 static void check(Program program) { |
61 program.accept(new VerifyingVisitor()); | 63 program.accept(new VerifyingVisitor()); |
62 } | 64 } |
63 | 65 |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 | 267 |
266 visitForInStatement(ForInStatement node) { | 268 visitForInStatement(ForInStatement node) { |
267 visitWithLocalScope(node); | 269 visitWithLocalScope(node); |
268 } | 270 } |
269 | 271 |
270 visitLet(Let node) { | 272 visitLet(Let node) { |
271 visitWithLocalScope(node); | 273 visitWithLocalScope(node); |
272 } | 274 } |
273 | 275 |
274 visitCatch(Catch node) { | 276 visitCatch(Catch node) { |
277 bool savedinCatchBlock = inCatchBlock; | |
278 inCatchBlock = true; | |
275 visitWithLocalScope(node); | 279 visitWithLocalScope(node); |
280 inCatchBlock = savedinCatchBlock; | |
asgerf
2017/04/07 08:52:52
Also save and set to false in visitFunctionNode.
ahe
2017/04/07 10:43:39
Good point. I know another file where I need to do
| |
281 } | |
282 | |
283 @override | |
284 visitRethrow(Rethrow node) { | |
285 if (!inCatchBlock) { | |
286 problem(node, "Rethrow must be inside a Catch block."); | |
287 } | |
276 } | 288 } |
277 | 289 |
278 visitVariableDeclaration(VariableDeclaration node) { | 290 visitVariableDeclaration(VariableDeclaration node) { |
291 var parent = node.parent; | |
292 if (parent is! Block && | |
293 !(parent is Catch && parent.body != node) && | |
294 !(parent is FunctionNode && parent.body != node) && | |
295 parent is! FunctionDeclaration && | |
296 !(parent is ForStatement && parent.body != node) && | |
297 !(parent is ForInStatement && parent.body != node) && | |
298 parent is! Let && | |
299 parent is! LocalInitializer) { | |
300 problem( | |
301 node, | |
302 "VariableDeclaration must be a direct child of a Block, " | |
303 "not ${parent.runtimeType}."); | |
304 } | |
279 visitChildren(node); | 305 visitChildren(node); |
280 declareVariable(node); | 306 declareVariable(node); |
281 } | 307 } |
282 | 308 |
283 visitVariableGet(VariableGet node) { | 309 visitVariableGet(VariableGet node) { |
284 checkVariableInScope(node.variable, node); | 310 checkVariableInScope(node.variable, node); |
285 visitChildren(node); | 311 visitChildren(node); |
286 } | 312 } |
287 | 313 |
288 visitVariableSet(VariableSet node) { | 314 visitVariableSet(VariableSet node) { |
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
513 var oldParent = parent; | 539 var oldParent = parent; |
514 parent = node; | 540 parent = node; |
515 node.visitChildren(this); | 541 node.visitChildren(this); |
516 parent = oldParent; | 542 parent = oldParent; |
517 } | 543 } |
518 } | 544 } |
519 | 545 |
520 void checkInitializers(Constructor constructor) { | 546 void checkInitializers(Constructor constructor) { |
521 // TODO(ahe): I'll add more here in other CLs. | 547 // TODO(ahe): I'll add more here in other CLs. |
522 } | 548 } |
OLD | NEW |