Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017, 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 import 'package:analyzer/dart/ast/ast.dart'; | |
| 6 import 'package:analyzer/dart/ast/visitor.dart'; | |
| 7 import 'package:analyzer/dart/element/type.dart'; | |
| 8 | |
| 9 /// Visitor that applies resolution data from the front end (obtained via | |
| 10 /// [ResolutionStorer]) to an analyzer AST. | |
| 11 class ResolutionApplier extends GeneralizingAstVisitor { | |
| 12 final List<DartType> _types; | |
| 13 int _typeIndex = 0; | |
| 14 | |
| 15 ResolutionApplier(this._types); | |
| 16 | |
| 17 /// Verifies that all types passed to the constructor have been applied. | |
| 18 void checkDone() { | |
| 19 if (_typeIndex != _types.length) { | |
| 20 throw new StateError( | |
| 21 'Some types were not consumed, starting at ${_types[_typeIndex]}'); | |
| 22 } | |
| 23 } | |
| 24 | |
| 25 @override | |
| 26 void visitExpression(Expression node) { | |
| 27 visitNode(node); | |
| 28 node.staticType = _getTypeFor(node); | |
| 29 } | |
| 30 | |
| 31 @override | |
| 32 void visitMethodInvocation(MethodInvocation node) { | |
| 33 node.target?.accept(this); | |
| 34 // TODO(paulberry): store resolution of node.methodName. | |
| 35 // TODO(paulberry): store resolution of node.typeArguments. | |
| 36 node.argumentList.accept(this); | |
| 37 node.staticType = _getTypeFor(node); | |
| 38 } | |
| 39 | |
| 40 @override | |
| 41 void visitNode(AstNode node) { | |
| 42 node.visitChildren(this); | |
|
scheglov
2017/07/12 21:43:46
I think this is the default behaviour of this meth
Paul Berry
2017/07/12 22:20:12
Good point, thank you. Removed.
| |
| 43 } | |
| 44 | |
| 45 DartType _getTypeFor(Expression node) { | |
| 46 return _types[_typeIndex++]; | |
| 47 } | |
| 48 } | |
| 49 | |
| 50 /// Visitor that applies resolution data from the front end (obtained via | |
| 51 /// [ResolutionStorer]) to an analyzer AST, and also checks file offsets to | |
| 52 /// verify that the types are applied to the correct subexpressions. | |
| 53 class ValidatingResolutionApplier extends ResolutionApplier { | |
| 54 /// Indicates whether debug messages should be printed. | |
| 55 static bool _debug = false; | |
|
scheglov
2017/07/12 21:43:46
We could also make it const, not sure if this matt
Paul Berry
2017/07/12 22:20:12
Done.
| |
| 56 | |
| 57 final List<int> _typeOffsets; | |
| 58 | |
| 59 ValidatingResolutionApplier(List<DartType> types, this._typeOffsets) | |
| 60 : super(types); | |
| 61 | |
| 62 @override | |
| 63 void checkDone() { | |
| 64 if (_typeIndex != _types.length) { | |
| 65 throw new StateError('Some types were not consumed, starting at offset ' | |
| 66 '${_typeOffsets[_typeIndex]}'); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 @override | |
| 71 DartType _getTypeFor(Expression node) { | |
| 72 if (_debug) print('Getting type for $node'); | |
| 73 if (node.offset != _typeOffsets[_typeIndex]) { | |
| 74 throw new StateError( | |
| 75 'Expected a type for offset ${node.offset}; got one for ' | |
| 76 '${_typeOffsets[_typeIndex]}'); | |
| 77 } | |
| 78 return super._getTypeFor(node); | |
| 79 } | |
| 80 } | |
| OLD | NEW |