| 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 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 | 336 |
| 337 /** | 337 /** |
| 338 * Serialize annotations from the given [element]. If [element] has no | 338 * Serialize annotations from the given [element]. If [element] has no |
| 339 * annotations, the empty list is returned. | 339 * annotations, the empty list is returned. |
| 340 */ | 340 */ |
| 341 List<UnlinkedConstBuilder> serializeAnnotations(Element element) { | 341 List<UnlinkedConstBuilder> serializeAnnotations(Element element) { |
| 342 if (element.metadata.isEmpty) { | 342 if (element.metadata.isEmpty) { |
| 343 return const <UnlinkedConstBuilder>[]; | 343 return const <UnlinkedConstBuilder>[]; |
| 344 } | 344 } |
| 345 return element.metadata.map((ElementAnnotationImpl a) { | 345 return element.metadata.map((ElementAnnotationImpl a) { |
| 346 _ConstExprSerializer serializer = new _ConstExprSerializer(this); | 346 _ConstExprSerializer serializer = new _ConstExprSerializer(this, null); |
| 347 serializer.serializeAnnotation(a.annotationAst); | 347 serializer.serializeAnnotation(a.annotationAst); |
| 348 return serializer.toBuilder(); | 348 return serializer.toBuilder(); |
| 349 }).toList(); | 349 }).toList(); |
| 350 } | 350 } |
| 351 | 351 |
| 352 /** | 352 /** |
| 353 * Serialize the given [classElement], creating an [UnlinkedClass]. | 353 * Serialize the given [classElement], creating an [UnlinkedClass]. |
| 354 */ | 354 */ |
| 355 UnlinkedClassBuilder serializeClass(ClassElement classElement) { | 355 UnlinkedClassBuilder serializeClass(ClassElement classElement) { |
| 356 UnlinkedClassBuilder b = new UnlinkedClassBuilder(); | 356 UnlinkedClassBuilder b = new UnlinkedClassBuilder(); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 b.shows = combinator.shownNames; | 448 b.shows = combinator.shownNames; |
| 449 } else if (combinator is HideElementCombinator) { | 449 } else if (combinator is HideElementCombinator) { |
| 450 b.hides = combinator.hiddenNames; | 450 b.hides = combinator.hiddenNames; |
| 451 } | 451 } |
| 452 return b; | 452 return b; |
| 453 } | 453 } |
| 454 | 454 |
| 455 /** | 455 /** |
| 456 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. | 456 * Serialize the given [expression], creating an [UnlinkedConstBuilder]. |
| 457 */ | 457 */ |
| 458 UnlinkedConstBuilder serializeConstExpr(Expression expression) { | 458 UnlinkedConstBuilder serializeConstExpr(Expression expression, |
| 459 _ConstExprSerializer serializer = new _ConstExprSerializer(this); | 459 [Set<String> constructorParameterNames]) { |
| 460 _ConstExprSerializer serializer = |
| 461 new _ConstExprSerializer(this, constructorParameterNames); |
| 460 serializer.serialize(expression); | 462 serializer.serialize(expression); |
| 461 return serializer.toBuilder(); | 463 return serializer.toBuilder(); |
| 462 } | 464 } |
| 463 | 465 |
| 464 /** | 466 /** |
| 465 * Serialize documentation from the given [element], creating an | 467 * Serialize documentation from the given [element], creating an |
| 466 * [UnlinkedDocumentationComment]. | 468 * [UnlinkedDocumentationComment]. |
| 467 * | 469 * |
| 468 * If [element] has no documentation, `null` is returned. | 470 * If [element] has no documentation, `null` is returned. |
| 469 */ | 471 */ |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 b.typeParameters = | 521 b.typeParameters = |
| 520 executableElement.typeParameters.map(serializeTypeParam).toList(); | 522 executableElement.typeParameters.map(serializeTypeParam).toList(); |
| 521 b.parameters = | 523 b.parameters = |
| 522 executableElement.type.parameters.map(serializeParam).toList(); | 524 executableElement.type.parameters.map(serializeParam).toList(); |
| 523 if (executableElement is PropertyAccessorElement) { | 525 if (executableElement is PropertyAccessorElement) { |
| 524 if (executableElement.isGetter) { | 526 if (executableElement.isGetter) { |
| 525 b.kind = UnlinkedExecutableKind.getter; | 527 b.kind = UnlinkedExecutableKind.getter; |
| 526 } else { | 528 } else { |
| 527 b.kind = UnlinkedExecutableKind.setter; | 529 b.kind = UnlinkedExecutableKind.setter; |
| 528 } | 530 } |
| 529 } else if (executableElement is ConstructorElement) { | 531 } else if (executableElement is ConstructorElementImpl) { |
| 530 b.kind = UnlinkedExecutableKind.constructor; | 532 b.kind = UnlinkedExecutableKind.constructor; |
| 531 b.isConst = executableElement.isConst; | 533 b.isConst = executableElement.isConst; |
| 532 b.isFactory = executableElement.isFactory; | 534 b.isFactory = executableElement.isFactory; |
| 535 if (executableElement.isConst && |
| 536 executableElement.constantInitializers != null) { |
| 537 Set<String> constructorParameterNames = |
| 538 executableElement.parameters.map((p) => p.name).toSet(); |
| 539 b.constantInitializers = executableElement.constantInitializers |
| 540 .map((ConstructorInitializer initializer) => |
| 541 serializeConstructorInitializer( |
| 542 initializer, |
| 543 (expr) => |
| 544 serializeConstExpr(expr, constructorParameterNames))) |
| 545 .toList(); |
| 546 } |
| 533 } else { | 547 } else { |
| 534 b.kind = UnlinkedExecutableKind.functionOrMethod; | 548 b.kind = UnlinkedExecutableKind.functionOrMethod; |
| 535 } | 549 } |
| 536 b.isAbstract = executableElement.isAbstract; | 550 b.isAbstract = executableElement.isAbstract; |
| 537 b.isStatic = executableElement.isStatic && | 551 b.isStatic = executableElement.isStatic && |
| 538 executableElement.enclosingElement is ClassElement; | 552 executableElement.enclosingElement is ClassElement; |
| 539 b.isExternal = executableElement.isExternal; | 553 b.isExternal = executableElement.isExternal; |
| 540 b.documentationComment = serializeDocumentation(executableElement); | 554 b.documentationComment = serializeDocumentation(executableElement); |
| 541 b.annotations = serializeAnnotations(executableElement); | 555 b.annotations = serializeAnnotations(executableElement); |
| 542 return b; | 556 return b; |
| (...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 936 } | 950 } |
| 937 } | 951 } |
| 938 | 952 |
| 939 /** | 953 /** |
| 940 * Instances of this class keep track of intermediate state during | 954 * Instances of this class keep track of intermediate state during |
| 941 * serialization of a single constant [Expression]. | 955 * serialization of a single constant [Expression]. |
| 942 */ | 956 */ |
| 943 class _ConstExprSerializer extends AbstractConstExprSerializer { | 957 class _ConstExprSerializer extends AbstractConstExprSerializer { |
| 944 final _CompilationUnitSerializer serializer; | 958 final _CompilationUnitSerializer serializer; |
| 945 | 959 |
| 946 _ConstExprSerializer(this.serializer); | 960 /** |
| 961 * If a constructor initializer expression is being serialized, the names of |
| 962 * the constructor parameters. Otherwise `null`. |
| 963 */ |
| 964 final Set<String> constructorParameterNames; |
| 965 |
| 966 _ConstExprSerializer(this.serializer, this.constructorParameterNames); |
| 947 | 967 |
| 948 @override | 968 @override |
| 949 void serializeAnnotation(Annotation annotation) { | 969 void serializeAnnotation(Annotation annotation) { |
| 950 if (annotation.arguments == null) { | 970 if (annotation.arguments == null) { |
| 951 assert(annotation.constructorName == null); | 971 assert(annotation.constructorName == null); |
| 952 serialize(annotation.name); | 972 serialize(annotation.name); |
| 953 } else { | 973 } else { |
| 954 Identifier name = annotation.name; | 974 Identifier name = annotation.name; |
| 955 Element nameElement = name.staticElement; | 975 Element nameElement = name.staticElement; |
| 956 EntityRefBuilder constructor; | 976 EntityRefBuilder constructor; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1024 } | 1044 } |
| 1025 return new EntityRefBuilder( | 1045 return new EntityRefBuilder( |
| 1026 reference: serializer._getElementReferenceId(element)); | 1046 reference: serializer._getElementReferenceId(element)); |
| 1027 } | 1047 } |
| 1028 | 1048 |
| 1029 @override | 1049 @override |
| 1030 EntityRefBuilder serializeType(TypeName typeName) { | 1050 EntityRefBuilder serializeType(TypeName typeName) { |
| 1031 DartType type = typeName != null ? typeName.type : DynamicTypeImpl.instance; | 1051 DartType type = typeName != null ? typeName.type : DynamicTypeImpl.instance; |
| 1032 return serializer.serializeTypeRef(type, null); | 1052 return serializer.serializeTypeRef(type, null); |
| 1033 } | 1053 } |
| 1054 |
| 1055 @override |
| 1056 bool isConstructorParameterName(String name) { |
| 1057 return constructorParameterNames?.contains(name) ?? false; |
| 1058 } |
| 1034 } | 1059 } |
| 1035 | 1060 |
| 1036 /** | 1061 /** |
| 1037 * Instances of this class keep track of intermediate state during | 1062 * Instances of this class keep track of intermediate state during |
| 1038 * serialization of a single library. | 1063 * serialization of a single library. |
| 1039 */ | 1064 */ |
| 1040 class _LibrarySerializer { | 1065 class _LibrarySerializer { |
| 1041 /** | 1066 /** |
| 1042 * The library to be serialized. | 1067 * The library to be serialized. |
| 1043 */ | 1068 */ |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1216 exportNames.add(new LinkedExportNameBuilder( | 1241 exportNames.add(new LinkedExportNameBuilder( |
| 1217 name: name, | 1242 name: name, |
| 1218 dependency: serializeDependency(dependentLibrary), | 1243 dependency: serializeDependency(dependentLibrary), |
| 1219 unit: unit, | 1244 unit: unit, |
| 1220 kind: kind)); | 1245 kind: kind)); |
| 1221 } | 1246 } |
| 1222 pb.exportNames = exportNames; | 1247 pb.exportNames = exportNames; |
| 1223 return pb; | 1248 return pb; |
| 1224 } | 1249 } |
| 1225 } | 1250 } |
| OLD | NEW |