| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 | 4 |
| 5 library analyzer.src.generated.resolver; | 5 library analyzer.src.generated.resolver; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart'; | 10 import 'package:analyzer/dart/ast/token.dart'; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 import 'package:analyzer/src/generated/source.dart'; | 29 import 'package:analyzer/src/generated/source.dart'; |
| 30 import 'package:analyzer/src/generated/static_type_analyzer.dart'; | 30 import 'package:analyzer/src/generated/static_type_analyzer.dart'; |
| 31 import 'package:analyzer/src/generated/type_system.dart'; | 31 import 'package:analyzer/src/generated/type_system.dart'; |
| 32 import 'package:analyzer/src/generated/utilities_dart.dart'; | 32 import 'package:analyzer/src/generated/utilities_dart.dart'; |
| 33 import 'package:analyzer/src/task/strong/info.dart' | 33 import 'package:analyzer/src/task/strong/info.dart' |
| 34 show InferredType, StaticInfo; | 34 show InferredType, StaticInfo; |
| 35 | 35 |
| 36 export 'package:analyzer/src/generated/type_system.dart'; | 36 export 'package:analyzer/src/generated/type_system.dart'; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * Throw an [ElementMismatchException] to report that the element model and the | |
| 40 * AST do not match. The [message] will have the path to the given [node] | |
| 41 * appended to it. | |
| 42 */ | |
| 43 void _mismatch(String message, AstNode node) { | |
| 44 StringBuffer buffer = new StringBuffer(); | |
| 45 buffer.writeln(message); | |
| 46 buffer.write('Path to root:'); | |
| 47 String separator = ' '; | |
| 48 AstNode parent = node; | |
| 49 while (parent != null) { | |
| 50 buffer.write(separator); | |
| 51 buffer.write(parent.runtimeType.toString()); | |
| 52 separator = ', '; | |
| 53 parent = parent.parent; | |
| 54 } | |
| 55 throw new ElementMismatchException(buffer.toString()); | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Instances of the class `BestPracticesVerifier` traverse an AST structure look
ing for | 39 * Instances of the class `BestPracticesVerifier` traverse an AST structure look
ing for |
| 60 * violations of Dart best practices. | 40 * violations of Dart best practices. |
| 61 */ | 41 */ |
| 62 class BestPracticesVerifier extends RecursiveAstVisitor<Object> { | 42 class BestPracticesVerifier extends RecursiveAstVisitor<Object> { |
| 63 // static String _HASHCODE_GETTER_NAME = "hashCode"; | 43 // static String _HASHCODE_GETTER_NAME = "hashCode"; |
| 64 | 44 |
| 65 static String _NULL_TYPE_NAME = "Null"; | 45 static String _NULL_TYPE_NAME = "Null"; |
| 66 | 46 |
| 67 static String _TO_INT_METHOD_NAME = "toInt"; | 47 static String _TO_INT_METHOD_NAME = "toInt"; |
| 68 | 48 |
| (...skipping 2097 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2166 | 2146 |
| 2167 /** | 2147 /** |
| 2168 * A visitor that resolves declarations in an AST structure to already built | 2148 * A visitor that resolves declarations in an AST structure to already built |
| 2169 * elements. | 2149 * elements. |
| 2170 * | 2150 * |
| 2171 * The resulting AST must have everything resolved that would have been resolved | 2151 * The resulting AST must have everything resolved that would have been resolved |
| 2172 * by a [CompilationUnitBuilder] (that is, must be a valid [RESOLVED_UNIT1]). | 2152 * by a [CompilationUnitBuilder] (that is, must be a valid [RESOLVED_UNIT1]). |
| 2173 * This class must not assume that the [CompilationUnitElement] passed to it is | 2153 * This class must not assume that the [CompilationUnitElement] passed to it is |
| 2174 * any more complete than a [COMPILATION_UNIT_ELEMENT]. | 2154 * any more complete than a [COMPILATION_UNIT_ELEMENT]. |
| 2175 */ | 2155 */ |
| 2176 class DeclarationResolver extends RecursiveAstVisitor<Object> { | 2156 class DeclarationResolver extends RecursiveAstVisitor<Object> |
| 2157 with ExistingElementResolver { |
| 2177 /** | 2158 /** |
| 2178 * The analysis context containing the sources to be analyzed. | 2159 * The analysis context containing the sources to be analyzed. |
| 2179 */ | 2160 */ |
| 2180 AnalysisContext _context; | 2161 AnalysisContext _context; |
| 2181 | 2162 |
| 2182 /** | 2163 /** |
| 2183 * The elements that are reachable from the compilation unit element. When a | 2164 * The elements that are reachable from the compilation unit element. When a |
| 2184 * compilation unit has been resolved, this set should be empty. | 2165 * compilation unit has been resolved, this set should be empty. |
| 2185 */ | 2166 */ |
| 2186 Set<Element> _expectedElements; | 2167 Set<Element> _expectedElements; |
| 2187 | 2168 |
| 2188 /** | 2169 /** |
| 2189 * The compilation unit containing the AST nodes being visited. | |
| 2190 */ | |
| 2191 CompilationUnitElementImpl _enclosingUnit; | |
| 2192 | |
| 2193 /** | |
| 2194 * The function type alias containing the AST nodes being visited, or `null` | 2170 * The function type alias containing the AST nodes being visited, or `null` |
| 2195 * if we are not in the scope of a function type alias. | 2171 * if we are not in the scope of a function type alias. |
| 2196 */ | 2172 */ |
| 2197 FunctionTypeAliasElement _enclosingAlias; | 2173 FunctionTypeAliasElement _enclosingAlias; |
| 2198 | 2174 |
| 2199 /** | 2175 /** |
| 2200 * The class containing the AST nodes being visited, or `null` if we are not | 2176 * The class containing the AST nodes being visited, or `null` if we are not |
| 2201 * in the scope of a class. | 2177 * in the scope of a class. |
| 2202 */ | 2178 */ |
| 2203 ClassElement _enclosingClass; | 2179 ClassElement _enclosingClass; |
| (...skipping 611 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2815 } | 2791 } |
| 2816 } | 2792 } |
| 2817 | 2793 |
| 2818 /** | 2794 /** |
| 2819 * A visitor that resolves directives in an AST structure to already built | 2795 * A visitor that resolves directives in an AST structure to already built |
| 2820 * elements. | 2796 * elements. |
| 2821 * | 2797 * |
| 2822 * The resulting AST must have everything resolved that would have been resolved | 2798 * The resulting AST must have everything resolved that would have been resolved |
| 2823 * by a [DirectiveElementBuilder]. | 2799 * by a [DirectiveElementBuilder]. |
| 2824 */ | 2800 */ |
| 2825 class DirectiveResolver extends SimpleAstVisitor { | 2801 class DirectiveResolver extends SimpleAstVisitor with ExistingElementResolver { |
| 2826 CompilationUnitElementImpl _enclosingUnit; | |
| 2827 | |
| 2828 @override | 2802 @override |
| 2829 void visitCompilationUnit(CompilationUnit node) { | 2803 void visitCompilationUnit(CompilationUnit node) { |
| 2830 _enclosingUnit = node.element; | 2804 _enclosingUnit = node.element; |
| 2831 for (Directive directive in node.directives) { | 2805 for (Directive directive in node.directives) { |
| 2832 directive.accept(this); | 2806 directive.accept(this); |
| 2833 } | 2807 } |
| 2834 } | 2808 } |
| 2835 | 2809 |
| 2836 @override | 2810 @override |
| 2837 void visitExportDirective(ExportDirective node) { | 2811 void visitExportDirective(ExportDirective node) { |
| (...skipping 638 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3476 new PropertyAccessorElementImpl.forVariable(field); | 3450 new PropertyAccessorElementImpl.forVariable(field); |
| 3477 getter.getter = true; | 3451 getter.getter = true; |
| 3478 getter.returnType = field.type; | 3452 getter.returnType = field.type; |
| 3479 getter.type = new FunctionTypeImpl(getter); | 3453 getter.type = new FunctionTypeImpl(getter); |
| 3480 field.getter = getter; | 3454 field.getter = getter; |
| 3481 return getter; | 3455 return getter; |
| 3482 } | 3456 } |
| 3483 } | 3457 } |
| 3484 | 3458 |
| 3485 /** | 3459 /** |
| 3460 * A mixin for classes that use an existing element model to resolve a portion |
| 3461 * of an AST structure. |
| 3462 */ |
| 3463 class ExistingElementResolver { |
| 3464 /** |
| 3465 * The compilation unit containing the AST nodes being visited. |
| 3466 */ |
| 3467 CompilationUnitElementImpl _enclosingUnit; |
| 3468 |
| 3469 /** |
| 3470 * Throw an [ElementMismatchException] to report that the element model and th
e |
| 3471 * AST do not match. The [message] will have the path to the given [node] |
| 3472 * appended to it. |
| 3473 */ |
| 3474 void _mismatch(String message, AstNode node) { |
| 3475 StringBuffer buffer = new StringBuffer(); |
| 3476 buffer.write('Mismatch in '); |
| 3477 buffer.write(runtimeType); |
| 3478 buffer.write(' while resolving '); |
| 3479 buffer.writeln(_enclosingUnit?.source?.fullName); |
| 3480 buffer.writeln(message); |
| 3481 buffer.write('Path to root:'); |
| 3482 String separator = ' '; |
| 3483 AstNode parent = node; |
| 3484 while (parent != null) { |
| 3485 buffer.write(separator); |
| 3486 buffer.write(parent.runtimeType.toString()); |
| 3487 separator = ', '; |
| 3488 parent = parent.parent; |
| 3489 } |
| 3490 throw new ElementMismatchException(buffer.toString()); |
| 3491 } |
| 3492 } |
| 3493 |
| 3494 /** |
| 3486 * Instances of the class `ExitDetector` determine whether the visited AST node
is guaranteed | 3495 * Instances of the class `ExitDetector` determine whether the visited AST node
is guaranteed |
| 3487 * to terminate by executing a `return` statement, `throw` expression, `rethrow` | 3496 * to terminate by executing a `return` statement, `throw` expression, `rethrow` |
| 3488 * expression, or simple infinite loop such as `while(true)`. | 3497 * expression, or simple infinite loop such as `while(true)`. |
| 3489 */ | 3498 */ |
| 3490 class ExitDetector extends GeneralizingAstVisitor<bool> { | 3499 class ExitDetector extends GeneralizingAstVisitor<bool> { |
| 3491 /** | 3500 /** |
| 3492 * Set to `true` when a `break` is encountered, and reset to `false` when a | 3501 * Set to `true` when a `break` is encountered, and reset to `false` when a |
| 3493 * `do`, `while`, `for` or `switch` block is entered. | 3502 * `do`, `while`, `for` or `switch` block is entered. |
| 3494 */ | 3503 */ |
| 3495 bool _enclosingBlockContainsBreak = false; | 3504 bool _enclosingBlockContainsBreak = false; |
| (...skipping 9455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 12951 nonFields.add(node); | 12960 nonFields.add(node); |
| 12952 return null; | 12961 return null; |
| 12953 } | 12962 } |
| 12954 | 12963 |
| 12955 @override | 12964 @override |
| 12956 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); | 12965 Object visitNode(AstNode node) => node.accept(TypeResolverVisitor_this); |
| 12957 | 12966 |
| 12958 @override | 12967 @override |
| 12959 Object visitWithClause(WithClause node) => null; | 12968 Object visitWithClause(WithClause node) => null; |
| 12960 } | 12969 } |
| OLD | NEW |