| 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); |
| 11 } | 11 } |
| 12 | 12 |
| 13 /// Checks that a kernel program is well-formed. | 13 /// Checks that a kernel program is well-formed. |
| 14 /// | 14 /// |
| 15 /// This does not include any kind of type checking. | 15 /// This does not include any kind of type checking. |
| 16 class VerifyingVisitor extends RecursiveVisitor { | 16 class VerifyingVisitor extends RecursiveVisitor { |
| 17 final Set<Class> classes = new Set<Class>(); | 17 final Set<Class> classes = new Set<Class>(); |
| 18 final Set<TypeParameter> typeParameters = new Set<TypeParameter>(); | 18 final Set<TypeParameter> typeParameters = new Set<TypeParameter>(); |
| 19 final List<VariableDeclaration> variableStack = <VariableDeclaration>[]; | 19 final List<VariableDeclaration> variableStack = <VariableDeclaration>[]; |
| 20 bool classTypeParametersAreInScope = false; | 20 bool classTypeParametersAreInScope = false; |
| 21 | 21 |
| 22 /// If true, relax certain checks for *outline* mode. For example, don't |
| 23 /// attempt to validate constructor initializers. |
| 24 bool isOutline = false; |
| 25 |
| 22 Member currentMember; | 26 Member currentMember; |
| 23 Class currentClass; | 27 Class currentClass; |
| 24 TreeNode currentParent; | 28 TreeNode currentParent; |
| 25 | 29 |
| 26 TreeNode get context => currentMember ?? currentClass; | 30 TreeNode get context => currentMember ?? currentClass; |
| 27 | 31 |
| 28 static void check(Program program) { | 32 static void check(Program program) { |
| 29 program.accept(new VerifyingVisitor()); | 33 program.accept(new VerifyingVisitor()); |
| 30 } | 34 } |
| 31 | 35 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 110 } |
| 107 | 111 |
| 108 void checkVariableInScope(VariableDeclaration variable, TreeNode where) { | 112 void checkVariableInScope(VariableDeclaration variable, TreeNode where) { |
| 109 if (variable.flags & VariableDeclaration.FlagInScope == 0) { | 113 if (variable.flags & VariableDeclaration.FlagInScope == 0) { |
| 110 throw 'Variable $variable used out of scope in $context ' | 114 throw 'Variable $variable used out of scope in $context ' |
| 111 '(${where.location})'; | 115 '(${where.location})'; |
| 112 } | 116 } |
| 113 } | 117 } |
| 114 | 118 |
| 115 visitProgram(Program program) { | 119 visitProgram(Program program) { |
| 116 for (var library in program.libraries) { | 120 try { |
| 117 for (var class_ in library.classes) { | 121 for (var library in program.libraries) { |
| 118 if (!classes.add(class_)) { | 122 for (var class_ in library.classes) { |
| 119 throw 'Class $class_ declared more than once'; | 123 if (!classes.add(class_)) { |
| 124 throw 'Class $class_ declared more than once.'; |
| 125 } |
| 126 } |
| 127 library.members.forEach(declareMember); |
| 128 for (var class_ in library.classes) { |
| 129 class_.members.forEach(declareMember); |
| 120 } | 130 } |
| 121 } | 131 } |
| 122 library.members.forEach(declareMember); | 132 visitChildren(program); |
| 123 for (var class_ in library.classes) { | 133 } finally { |
| 124 class_.members.forEach(declareMember); | 134 for (var library in program.libraries) { |
| 135 library.members.forEach(undeclareMember); |
| 136 for (var class_ in library.classes) { |
| 137 class_.members.forEach(undeclareMember); |
| 138 } |
| 125 } | 139 } |
| 126 } | 140 variableStack.forEach(undeclareVariable); |
| 127 visitChildren(program); | |
| 128 for (var library in program.libraries) { | |
| 129 library.members.forEach(undeclareMember); | |
| 130 for (var class_ in library.classes) { | |
| 131 class_.members.forEach(undeclareMember); | |
| 132 } | |
| 133 } | 141 } |
| 134 } | 142 } |
| 135 | 143 |
| 136 visitField(Field node) { | 144 visitField(Field node) { |
| 137 currentMember = node; | 145 currentMember = node; |
| 138 var oldParent = enterParent(node); | 146 var oldParent = enterParent(node); |
| 139 classTypeParametersAreInScope = !node.isStatic; | 147 classTypeParametersAreInScope = !node.isStatic; |
| 140 node.initializer?.accept(this); | 148 node.initializer?.accept(this); |
| 141 classTypeParametersAreInScope = false; | 149 classTypeParametersAreInScope = false; |
| 142 visitList(node.annotations, this); | 150 visitList(node.annotations, this); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 157 | 165 |
| 158 visitConstructor(Constructor node) { | 166 visitConstructor(Constructor node) { |
| 159 currentMember = node; | 167 currentMember = node; |
| 160 classTypeParametersAreInScope = true; | 168 classTypeParametersAreInScope = true; |
| 161 // The constructor member needs special treatment due to parameters being | 169 // The constructor member needs special treatment due to parameters being |
| 162 // in scope in the initializer list. | 170 // in scope in the initializer list. |
| 163 var oldParent = enterParent(node); | 171 var oldParent = enterParent(node); |
| 164 int stackHeight = enterLocalScope(); | 172 int stackHeight = enterLocalScope(); |
| 165 visitChildren(node.function); | 173 visitChildren(node.function); |
| 166 visitList(node.initializers, this); | 174 visitList(node.initializers, this); |
| 175 if (!isOutline) { |
| 176 checkInitializers(node); |
| 177 } |
| 167 exitLocalScope(stackHeight); | 178 exitLocalScope(stackHeight); |
| 168 classTypeParametersAreInScope = false; | 179 classTypeParametersAreInScope = false; |
| 169 visitList(node.annotations, this); | 180 visitList(node.annotations, this); |
| 170 exitParent(oldParent); | 181 exitParent(oldParent); |
| 171 classTypeParametersAreInScope = false; | 182 classTypeParametersAreInScope = false; |
| 172 currentMember = null; | 183 currentMember = null; |
| 173 } | 184 } |
| 174 | 185 |
| 175 visitClass(Class node) { | 186 visitClass(Class node) { |
| 176 currentClass = node; | 187 currentClass = node; |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 throw 'Parent pointer on ${node.runtimeType} ' | 452 throw 'Parent pointer on ${node.runtimeType} ' |
| 442 'is ${node.parent.runtimeType} ' | 453 'is ${node.parent.runtimeType} ' |
| 443 'but should be ${parent.runtimeType}'; | 454 'but should be ${parent.runtimeType}'; |
| 444 } | 455 } |
| 445 var oldParent = parent; | 456 var oldParent = parent; |
| 446 parent = node; | 457 parent = node; |
| 447 node.visitChildren(this); | 458 node.visitChildren(this); |
| 448 parent = oldParent; | 459 parent = oldParent; |
| 449 } | 460 } |
| 450 } | 461 } |
| 462 |
| 463 void checkInitializers(Constructor constructor) { |
| 464 // TODO(ahe): I'll add more here in other CLs. |
| 465 } |
| OLD | NEW |