| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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.elements; | 5 library serialization.elements; |
| 6 | 6 |
| 7 import 'package:analyzer/dart/element/element.dart'; | 7 import 'package:analyzer/dart/element/element.dart'; |
| 8 import 'package:analyzer/dart/element/type.dart'; | 8 import 'package:analyzer/dart/element/type.dart'; |
| 9 import 'package:analyzer/src/dart/element/element.dart'; | 9 import 'package:analyzer/src/dart/element/element.dart'; |
| 10 import 'package:analyzer/src/dart/element/type.dart'; | 10 import 'package:analyzer/src/dart/element/type.dart'; |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 b.shows = combinator.shownNames; | 429 b.shows = combinator.shownNames; |
| 430 } else if (combinator is HideElementCombinator) { | 430 } else if (combinator is HideElementCombinator) { |
| 431 b.hides = combinator.hiddenNames; | 431 b.hides = combinator.hiddenNames; |
| 432 } | 432 } |
| 433 return b; | 433 return b; |
| 434 } | 434 } |
| 435 | 435 |
| 436 /** | 436 /** |
| 437 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. | 437 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. |
| 438 */ | 438 */ |
| 439 UnlinkedConstBuilder serializeConstExpr(Expression expression) { | 439 UnlinkedConstBuilder serializeConstExpr(Expression expression, |
| 440 _ConstExprSerializer serializer = new _ConstExprSerializer(this); | 440 [Set<String> constructorParameterNames]) { |
| 441 _ConstExprSerializer serializer = |
| 442 new _ConstExprSerializer(this, constructorParameterNames); |
| 441 serializer.serialize(expression); | 443 serializer.serialize(expression); |
| 442 return serializer.toBuilder(); | 444 return serializer.toBuilder(); |
| 443 } | 445 } |
| 444 | 446 |
| 445 /** | 447 /** |
| 446 * Serialize documentation from the given [element], creating an | 448 * Serialize documentation from the given [element], creating an |
| 447 * [UnlinkedDocumentationComment]. | 449 * [UnlinkedDocumentationComment]. |
| 448 * | 450 * |
| 449 * If [element] has no documentation, `null` is returned. | 451 * If [element] has no documentation, `null` is returned. |
| 450 */ | 452 */ |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 b.typeParameters = | 501 b.typeParameters = |
| 500 executableElement.typeParameters.map(serializeTypeParam).toList(); | 502 executableElement.typeParameters.map(serializeTypeParam).toList(); |
| 501 b.parameters = | 503 b.parameters = |
| 502 executableElement.type.parameters.map(serializeParam).toList(); | 504 executableElement.type.parameters.map(serializeParam).toList(); |
| 503 if (executableElement is PropertyAccessorElement) { | 505 if (executableElement is PropertyAccessorElement) { |
| 504 if (executableElement.isGetter) { | 506 if (executableElement.isGetter) { |
| 505 b.kind = UnlinkedExecutableKind.getter; | 507 b.kind = UnlinkedExecutableKind.getter; |
| 506 } else { | 508 } else { |
| 507 b.kind = UnlinkedExecutableKind.setter; | 509 b.kind = UnlinkedExecutableKind.setter; |
| 508 } | 510 } |
| 509 } else if (executableElement is ConstructorElement) { | 511 } else if (executableElement is ConstructorElementImpl) { |
| 510 b.kind = UnlinkedExecutableKind.constructor; | 512 b.kind = UnlinkedExecutableKind.constructor; |
| 511 b.isConst = executableElement.isConst; | 513 b.isConst = executableElement.isConst; |
| 512 b.isFactory = executableElement.isFactory; | 514 b.isFactory = executableElement.isFactory; |
| 515 if (executableElement.isConst && |
| 516 executableElement.constantInitializers != null) { |
| 517 Set<String> constructorParameterNames = |
| 518 executableElement.parameters.map((p) => p.name).toSet(); |
| 519 b.constantInitializers = executableElement.constantInitializers |
| 520 .map((ConstructorInitializer initializer) => |
| 521 serializeConstructorInitializer( |
| 522 initializer, |
| 523 (expr) => |
| 524 serializeConstExpr(expr, constructorParameterNames))) |
| 525 .toList(); |
| 526 } |
| 513 } else { | 527 } else { |
| 514 b.kind = UnlinkedExecutableKind.functionOrMethod; | 528 b.kind = UnlinkedExecutableKind.functionOrMethod; |
| 515 } | 529 } |
| 516 b.isAbstract = executableElement.isAbstract; | 530 b.isAbstract = executableElement.isAbstract; |
| 517 b.isStatic = executableElement.isStatic && | 531 b.isStatic = executableElement.isStatic && |
| 518 executableElement.enclosingElement is ClassElement; | 532 executableElement.enclosingElement is ClassElement; |
| 519 b.isExternal = executableElement.isExternal; | 533 b.isExternal = executableElement.isExternal; |
| 520 b.documentationComment = serializeDocumentation(executableElement); | 534 b.documentationComment = serializeDocumentation(executableElement); |
| 521 return b; | 535 return b; |
| 522 } | 536 } |
| (...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 } | 923 } |
| 910 } | 924 } |
| 911 | 925 |
| 912 /** | 926 /** |
| 913 * Instances of this class keep track of intermediate state during | 927 * Instances of this class keep track of intermediate state during |
| 914 * serialization of a single constant [Expression]. | 928 * serialization of a single constant [Expression]. |
| 915 */ | 929 */ |
| 916 class _ConstExprSerializer extends AbstractConstExprSerializer { | 930 class _ConstExprSerializer extends AbstractConstExprSerializer { |
| 917 final _CompilationUnitSerializer serializer; | 931 final _CompilationUnitSerializer serializer; |
| 918 | 932 |
| 919 _ConstExprSerializer(this.serializer); | 933 /** |
| 934 * If a constructor initializer expression is being serialized, the names of |
| 935 * the constructor parameters. Otherwise `null`. |
| 936 */ |
| 937 final Set<String> constructorParameterNames; |
| 938 |
| 939 _ConstExprSerializer(this.serializer, this.constructorParameterNames); |
| 920 | 940 |
| 921 @override | 941 @override |
| 922 EntityRefBuilder serializeConstructorName(ConstructorName constructor) { | 942 EntityRefBuilder serializeConstructorName(ConstructorName constructor) { |
| 923 DartType type = constructor.type.type; | 943 DartType type = constructor.type.type; |
| 924 EntityRefBuilder typeRef = serializer.serializeTypeRef(type, null); | 944 EntityRefBuilder typeRef = serializer.serializeTypeRef(type, null); |
| 925 if (constructor.name == null) { | 945 if (constructor.name == null) { |
| 926 return typeRef; | 946 return typeRef; |
| 927 } else { | 947 } else { |
| 928 int typeId = typeRef.reference; | 948 int typeId = typeRef.reference; |
| 929 LinkedReference typeLinkedRef = serializer.linkedReferences[typeId]; | 949 LinkedReference typeLinkedRef = serializer.linkedReferences[typeId]; |
| 930 serializer.unlinkedReferences.add(new UnlinkedReferenceBuilder( | 950 serializer.unlinkedReferences.add(new UnlinkedReferenceBuilder( |
| 931 name: constructor.name.name, prefixReference: typeId)); | 951 name: constructor.name.name, prefixReference: typeId)); |
| 932 int refId = serializer.linkedReferences.length; | 952 int refId = serializer.linkedReferences.length; |
| 933 serializer.linkedReferences.add(new LinkedReferenceBuilder( | 953 serializer.linkedReferences.add(new LinkedReferenceBuilder( |
| 934 kind: ReferenceKind.constructor, | 954 kind: ReferenceKind.constructor, unit: typeLinkedRef.unit)); |
| 935 unit: typeLinkedRef.unit)); | |
| 936 return new EntityRefBuilder( | 955 return new EntityRefBuilder( |
| 937 reference: refId, typeArguments: typeRef.typeArguments); | 956 reference: refId, typeArguments: typeRef.typeArguments); |
| 938 } | 957 } |
| 939 } | 958 } |
| 940 | 959 |
| 941 EntityRefBuilder serializeIdentifier(Identifier identifier) { | 960 EntityRefBuilder serializeIdentifier(Identifier identifier) { |
| 942 Element element = identifier.staticElement; | 961 Element element = identifier.staticElement; |
| 943 assert(element != null); | 962 assert(element != null); |
| 944 // The only supported instance property accessor - `length`. | 963 // The only supported instance property accessor - `length`. |
| 945 if (identifier is PrefixedIdentifier && | 964 if (identifier is PrefixedIdentifier && |
| (...skipping 26 matching lines...) Expand all Loading... |
| 972 } | 991 } |
| 973 return new EntityRefBuilder( | 992 return new EntityRefBuilder( |
| 974 reference: serializer._getElementReferenceId(element)); | 993 reference: serializer._getElementReferenceId(element)); |
| 975 } | 994 } |
| 976 | 995 |
| 977 @override | 996 @override |
| 978 EntityRefBuilder serializeType(TypeName typeName) { | 997 EntityRefBuilder serializeType(TypeName typeName) { |
| 979 DartType type = typeName != null ? typeName.type : DynamicTypeImpl.instance; | 998 DartType type = typeName != null ? typeName.type : DynamicTypeImpl.instance; |
| 980 return serializer.serializeTypeRef(type, null); | 999 return serializer.serializeTypeRef(type, null); |
| 981 } | 1000 } |
| 1001 |
| 1002 @override |
| 1003 bool isConstructorParameterName(String name) { |
| 1004 return constructorParameterNames?.contains(name) ?? false; |
| 1005 } |
| 982 } | 1006 } |
| 983 | 1007 |
| 984 /** | 1008 /** |
| 985 * Instances of this class keep track of intermediate state during | 1009 * Instances of this class keep track of intermediate state during |
| 986 * serialization of a single library. | 1010 * serialization of a single library. |
| 987 */ | 1011 */ |
| 988 class _LibrarySerializer { | 1012 class _LibrarySerializer { |
| 989 /** | 1013 /** |
| 990 * The library to be serialized. | 1014 * The library to be serialized. |
| 991 */ | 1015 */ |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1164 exportNames.add(new LinkedExportNameBuilder( | 1188 exportNames.add(new LinkedExportNameBuilder( |
| 1165 name: name, | 1189 name: name, |
| 1166 dependency: serializeDependency(dependentLibrary), | 1190 dependency: serializeDependency(dependentLibrary), |
| 1167 unit: unit, | 1191 unit: unit, |
| 1168 kind: kind)); | 1192 kind: kind)); |
| 1169 } | 1193 } |
| 1170 pb.exportNames = exportNames; | 1194 pb.exportNames = exportNames; |
| 1171 return pb; | 1195 return pb; |
| 1172 } | 1196 } |
| 1173 } | 1197 } |
| OLD | NEW |