Chromium Code Reviews| 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 | 4 |
| 5 library serialization.summarize_ast; | 5 library serialization.summarize_ast; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/ast/ast.dart'; | 7 import 'package:analyzer/dart/ast/ast.dart'; |
| 8 import 'package:analyzer/dart/ast/token.dart'; | 8 import 'package:analyzer/dart/ast/token.dart'; |
| 9 import 'package:analyzer/dart/ast/visitor.dart'; | 9 import 'package:analyzer/dart/ast/visitor.dart'; |
| 10 import 'package:analyzer/dart/element/type.dart' show DartType; | 10 import 'package:analyzer/dart/element/type.dart' show DartType; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 } else { | 92 } else { |
| 93 localIndex = localClosureIndexMap[functionExpression.offset]; | 93 localIndex = localClosureIndexMap[functionExpression.offset]; |
| 94 assert(localIndex != null); | 94 assert(localIndex != null); |
| 95 return <int>[0, localIndex]; | 95 return <int>[0, localIndex]; |
| 96 } | 96 } |
| 97 } | 97 } |
| 98 | 98 |
| 99 EntityRefBuilder serializeIdentifier(Identifier identifier) { | 99 EntityRefBuilder serializeIdentifier(Identifier identifier) { |
| 100 EntityRefBuilder b = new EntityRefBuilder(); | 100 EntityRefBuilder b = new EntityRefBuilder(); |
| 101 if (identifier is SimpleIdentifier) { | 101 if (identifier is SimpleIdentifier) { |
| 102 int index = visitor.serializeSimpleReference(identifier.name, | 102 int index = visitor.serializeSimpleReference(identifier.name); |
| 103 allowTypeParameter: true); | |
| 104 if (index < 0) { | 103 if (index < 0) { |
| 105 b.paramReference = -index; | 104 b.paramReference = -index; |
| 106 } else { | 105 } else { |
| 107 b.reference = index; | 106 b.reference = index; |
| 108 } | 107 } |
| 109 } else if (identifier is PrefixedIdentifier) { | 108 } else if (identifier is PrefixedIdentifier) { |
| 110 int prefix = visitor.serializeSimpleReference(identifier.prefix.name); | 109 int prefix = visitor.serializeSimpleReference(identifier.prefix.name); |
| 111 b.reference = | 110 b.reference = |
|
Paul Berry
2017/01/30 20:54:52
Need to add error handling code here in case seria
scheglov
2017/01/30 21:29:39
Done.
| |
| 112 visitor.serializeReference(prefix, identifier.identifier.name); | 111 visitor.serializeReference(prefix, identifier.identifier.name); |
| 113 } else { | 112 } else { |
| 114 throw new StateError( | 113 throw new StateError( |
| 115 'Unexpected identifier type: ${identifier.runtimeType}'); | 114 'Unexpected identifier type: ${identifier.runtimeType}'); |
| 116 } | 115 } |
| 117 return b; | 116 return b; |
| 118 } | 117 } |
| 119 | 118 |
| 120 @override | 119 @override |
| 121 EntityRefBuilder serializeIdentifierSequence(Expression expr) { | 120 EntityRefBuilder serializeIdentifierSequence(Expression expr) { |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 259 */ | 258 */ |
| 260 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[]; | 259 final List<UnlinkedImportBuilder> unlinkedImports = <UnlinkedImportBuilder>[]; |
| 261 | 260 |
| 262 /** | 261 /** |
| 263 * The unlinked portion of the "references table". This is the list of | 262 * The unlinked portion of the "references table". This is the list of |
| 264 * objects which should be written to [UnlinkedUnit.references]. | 263 * objects which should be written to [UnlinkedUnit.references]. |
| 265 */ | 264 */ |
| 266 final List<UnlinkedReferenceBuilder> unlinkedReferences = | 265 final List<UnlinkedReferenceBuilder> unlinkedReferences = |
| 267 <UnlinkedReferenceBuilder>[new UnlinkedReferenceBuilder()]; | 266 <UnlinkedReferenceBuilder>[new UnlinkedReferenceBuilder()]; |
| 268 | 267 |
| 269 | |
| 270 /** | 268 /** |
| 271 * List of [_Scope]s currently in effect. This is used to resolve type names | 269 * List of [_Scope]s currently in effect. This is used to resolve type names |
| 272 * to type parameters within classes, typedefs, and executables, as well as | 270 * to type parameters within classes, typedefs, and executables, as well as |
| 273 * references to class members. | 271 * references to class members. |
| 274 */ | 272 */ |
| 275 final List<_Scope> scopes = <_Scope>[]; | 273 final List<_Scope> scopes = <_Scope>[]; |
| 276 | 274 |
| 277 /** | 275 /** |
| 278 * True if 'dart:core' has been explicitly imported. | 276 * True if 'dart:core' has been explicitly imported. |
| 279 */ | 277 */ |
| (...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 835 int index = unlinkedReferences.length; | 833 int index = unlinkedReferences.length; |
| 836 unlinkedReferences.add(new UnlinkedReferenceBuilder( | 834 unlinkedReferences.add(new UnlinkedReferenceBuilder( |
| 837 prefixReference: prefixIndex, name: name)); | 835 prefixReference: prefixIndex, name: name)); |
| 838 return index; | 836 return index; |
| 839 }); | 837 }); |
| 840 | 838 |
| 841 /** | 839 /** |
| 842 * Serialize a reference to a name declared either at top level or in a | 840 * Serialize a reference to a name declared either at top level or in a |
| 843 * nested scope. | 841 * nested scope. |
| 844 * | 842 * |
| 845 * If [allowTypeParameter] is `true`, then references to type | 843 * References to type parameters are returned as negative numbers. |
| 846 * parameters are allowed, and are returned as negative numbers. | |
| 847 */ | 844 */ |
| 848 int serializeSimpleReference(String name, {bool allowTypeParameter: false}) { | 845 int serializeSimpleReference(String name) { |
| 849 int indexOffset = 0; | 846 int indexOffset = 0; |
| 850 for (int i = scopes.length - 1; i >= 0; i--) { | 847 for (int i = scopes.length - 1; i >= 0; i--) { |
| 851 _Scope scope = scopes[i]; | 848 _Scope scope = scopes[i]; |
| 852 _ScopedEntity entity = scope[name]; | 849 _ScopedEntity entity = scope[name]; |
| 853 if (entity != null) { | 850 if (entity != null) { |
| 854 if (entity is _ScopedClassMember) { | 851 if (entity is _ScopedClassMember) { |
| 855 return serializeReference( | 852 return serializeReference( |
| 856 serializeReference(null, entity.className), name); | 853 serializeReference(null, entity.className), name); |
| 857 } else if (allowTypeParameter && entity is _ScopedTypeParameter) { | 854 } else if (entity is _ScopedTypeParameter) { |
| 858 int paramReference = indexOffset + entity.index; | 855 int paramReference = indexOffset + entity.index; |
| 859 return -paramReference; | 856 return -paramReference; |
| 860 } else { | |
| 861 // Invalid reference to a type parameter. Should never happen in | |
| 862 // legal Dart code. | |
| 863 // TODO(paulberry): could this exception ever be uncaught in illegal | |
| 864 // code? | |
| 865 throw new StateError('Invalid identifier reference'); | |
| 866 } | 857 } |
| 867 } | 858 } |
| 868 if (scope is _TypeParameterScope) { | 859 if (scope is _TypeParameterScope) { |
| 869 indexOffset += scope.length; | 860 indexOffset += scope.length; |
| 870 } | 861 } |
| 871 } | 862 } |
| 872 return serializeReference(null, name); | 863 return serializeReference(null, name); |
| 873 } | 864 } |
| 874 | 865 |
| 875 /** | 866 /** |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 902 return b; | 893 return b; |
| 903 } | 894 } |
| 904 } | 895 } |
| 905 if (scope is _TypeParameterScope) { | 896 if (scope is _TypeParameterScope) { |
| 906 indexOffset += scope.length; | 897 indexOffset += scope.length; |
| 907 } | 898 } |
| 908 } | 899 } |
| 909 b.reference = serializeReference(null, name); | 900 b.reference = serializeReference(null, name); |
| 910 } else if (identifier is PrefixedIdentifier) { | 901 } else if (identifier is PrefixedIdentifier) { |
| 911 int prefixIndex = serializeSimpleReference(identifier.prefix.name); | 902 int prefixIndex = serializeSimpleReference(identifier.prefix.name); |
| 912 b.reference = | 903 if (prefixIndex < 0) { |
| 913 serializeReference(prefixIndex, identifier.identifier.name); | 904 // Type parameters are not expected here, so this is an error and the |
| 905 // type should be treated as a reference to `dynamic`. | |
| 906 b.reference = serializeReference(null, 'dynamic'); | |
| 907 return b; | |
| 908 } else { | |
| 909 b.reference = | |
| 910 serializeReference(prefixIndex, identifier.identifier.name); | |
| 911 } | |
| 914 } else { | 912 } else { |
| 915 throw new StateError( | 913 throw new StateError( |
| 916 'Unexpected identifier type: ${identifier.runtimeType}'); | 914 'Unexpected identifier type: ${identifier.runtimeType}'); |
| 917 } | 915 } |
| 918 if (typeArguments != null) { | 916 if (typeArguments != null) { |
| 919 b.typeArguments = | 917 b.typeArguments = |
| 920 typeArguments.arguments.map(serializeTypeName).toList(); | 918 typeArguments.arguments.map(serializeTypeName).toList(); |
| 921 } | 919 } |
| 922 return b; | 920 return b; |
| 923 } | 921 } |
| (...skipping 490 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1414 /** | 1412 /** |
| 1415 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. | 1413 * A [_TypeParameterScope] is a [_Scope] which defines [_ScopedTypeParameter]s. |
| 1416 */ | 1414 */ |
| 1417 class _TypeParameterScope extends _Scope { | 1415 class _TypeParameterScope extends _Scope { |
| 1418 /** | 1416 /** |
| 1419 * Get the number of [_ScopedTypeParameter]s defined in this | 1417 * Get the number of [_ScopedTypeParameter]s defined in this |
| 1420 * [_TypeParameterScope]. | 1418 * [_TypeParameterScope]. |
| 1421 */ | 1419 */ |
| 1422 int get length => _definedNames.length; | 1420 int get length => _definedNames.length; |
| 1423 } | 1421 } |
| OLD | NEW |