OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library analyzer.src.task.strong_mode; |
| 6 |
| 7 import 'package:analyzer/src/generated/ast.dart'; |
| 8 import 'package:analyzer/src/generated/element.dart'; |
| 9 |
| 10 /** |
| 11 * An object used to find static variables whose types should be inferred and |
| 12 * classes whose members should have types inferred. Clients are expected to |
| 13 * visit a [CompilationUnit]. |
| 14 */ |
| 15 class InferrenceFinder extends SimpleAstVisitor { |
| 16 /** |
| 17 * The static variables that should have types inferred for them. |
| 18 */ |
| 19 final List<VariableElement> staticVariables = <VariableElement>[]; |
| 20 |
| 21 /** |
| 22 * The classes defined in the unit. |
| 23 * |
| 24 * TODO(brianwilkerson) We don't currently remove classes whose members do not |
| 25 * need to be processed, but we potentially could. |
| 26 */ |
| 27 final List<ClassElement> classes = <ClassElement>[]; |
| 28 |
| 29 /** |
| 30 * Initialize a newly created finder. |
| 31 */ |
| 32 InferrenceFinder(); |
| 33 |
| 34 @override |
| 35 void visitClassDeclaration(ClassDeclaration node) { |
| 36 classes.add(node.element); |
| 37 for (ClassMember member in node.members) { |
| 38 member.accept(this); |
| 39 } |
| 40 } |
| 41 |
| 42 @override |
| 43 void visitClassTypeAlias(ClassTypeAlias node) { |
| 44 classes.add(node.element); |
| 45 } |
| 46 |
| 47 @override |
| 48 void visitCompilationUnit(CompilationUnit node) { |
| 49 for (CompilationUnitMember declaration in node.declarations) { |
| 50 declaration.accept(this); |
| 51 } |
| 52 } |
| 53 |
| 54 @override |
| 55 void visitFieldDeclaration(FieldDeclaration node) { |
| 56 if (node.isStatic && node.fields.type == null) { |
| 57 _addVariables(node.fields.variables); |
| 58 } |
| 59 } |
| 60 |
| 61 @override |
| 62 void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) { |
| 63 if (node.variables.type == null) { |
| 64 _addVariables(node.variables.variables); |
| 65 } |
| 66 } |
| 67 |
| 68 /** |
| 69 * Add all of the [variables] with initializers to the list of variables whose |
| 70 * type can be inferred. Technically, we only infer the types of variables |
| 71 * that do not have a static type, but all variables with initializers |
| 72 * potentially need to be re-resolved after inference because they might |
| 73 * refer to fields whose type was inferred. |
| 74 */ |
| 75 void _addVariables(NodeList<VariableDeclaration> variables) { |
| 76 for (VariableDeclaration variable in variables) { |
| 77 if (variable.initializer != null) { |
| 78 staticVariables.add(variable.element); |
| 79 } |
| 80 } |
| 81 } |
| 82 } |
OLD | NEW |